Skip to content

debug.setupvalue

C closures not supported

This function will throw an error if called on a C closure, such as print, for security reasons.

debug.setupvalue replaces an upvalue at the specified index in a Luau function, with a new value.

This allows for controlled modification of function state, often used in hooking or testing environments.

function debug.setupvalue(func: (...any) -> (...any) | number, index: number, value: any): ()

Parameters

Parameter Description
func The function (or stack level) whose upvalue to replace.
index The index of the upvalue to be replaced.
value The new value to assign to the upvalue.

Example

Replacing a numeric upvalue
local upvalue = 90

local function dummy_function()
    upvalue += 1
    print(upvalue)
end

dummy_function() -- Output: 91

debug.setupvalue(dummy_function, 1, 99)
dummy_function() -- Output: 100