Skip to content

debug.getupvalue

C closures are not supported

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

debug.getupvalue returns the upvalue at the specified index from a Luau function's closure. If the index is invalid or out of bounds, an error will occur.

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

Parameters

Parameter Description
func The Lua function (or stack level) to retrieve an upvalue from.
index The position of the upvalue.

Examples

Example 1

Retrieving a function upvalue
local UpFunction = function()
    print("Hello from up")
end

local function DummyFunction()
    UpFunction()
end

local Retrieved = debug.getupvalue(DummyFunction, 1)
Retrieved() -- Output: Hello from up

Example 2

Invalid index on a function with no upvalues
1
2
3
local function DummyFunction() end

debug.getupvalue(DummyFunction, 0) -- Should error

Example 3

Calling on a C closure should error
debug.getupvalue(print, 1) -- Should error due to C closure