debug.setupvalue
Replaces an upvalue in a function.
function debug.setupvalue(func: function | number, index: number, value: any): ()Synopsis
How it works
Writes a new value to a closureClosureA function value that captures its lexical environment. In Luau, closures come in two types: LClosure (Luau bytecode + Proto + upvalues) and CClosure (native C function pointer + upvalues).’s upvalueUpvalueA variable captured from an enclosing scope. When a function references a local variable from an outer function, that variable becomes an upvalue — stored in an UpVal struct that persists even after the outer function returns. at the specified index. Internally writes through the
UpVal->v pointer, which may point into a parent’s stack frameCall stackThe chain of CallInfo frames representing nested function calls. Each frame records the closure, base register, top, and saved program counter. Walking the call stack reveals the full execution trace. (open) or into heap storage (closed).Shared upvalue writes
When multiple inner closures capture the same outer local, they share the same
UpVal object. Setting an upvalueUpvalueA variable captured from an enclosing scope. When a function references a local variable from an outer function, that variable becomes an upvalue — stored in an UpVal struct that persists even after the outer function returns. on one closureClosureA function value that captures its lexical environment. In Luau, closures come in two types: LClosure (Luau bytecode + Proto + upvalues) and CClosure (native C function pointer + upvalues). writes to the shared storage, affecting all sibling closures that reference it. This is the most targeted and least detectable way to modify game behavior — you change an inner variable without modifying the function’s bytecodeBytecodeThe compiled binary representation of Luau source code. A sequence of 32-bit instructions (opcodes + operands) stored in a Proto's code[] array. Executed by the Luau VM interpreter. or swapping its Proto.Usage
Replace an upvalue
local function somethingImportant() print("Goodbye, world!") end
local function foo() somethingImportant() end
debug.setupvalue(foo, 1, function() print("Hello, world!") end)
foo() "cc">--> Hello, world!Parameters
func function | number A Luau function or stack level.
index number Upvalue index to replace.
value any New value.