debug.setproto Synchronous

Replace one nested prototype reference in a Luau function. It affects closures created from that nested slot after the change, so retain a matching original closure if you plan to restore it.

Syntax
Luau
debug.setproto(target: Function | number, index: number, replacement: Function) -> ()

Parameters

Function parameters
ParameterTypeDescription
targetFunction | numberLuau function or visible stack level containing the nested prototype.
indexnumberOne-based child prototype slot.
replacementFunctionLuau function with the required upvalue count.

Returns

()

No value.

Usage notes

Replacement upvalue count must match the target prototype’s upvalue count.

Example

Example
Luau
local function factory() return function() return "before" end end
local original = factory()
debug.setproto(factory, 1, function() return "after" end)
print(factory()()) -- after
debug.setproto(factory, 1, original)
Kawaii documentation