Skip to content

debug.getconstant

C closures are not supported

This function will throw an error if called on a C closure, such as print, since C closures have no accessible constants.

debug.getconstant returns the constant at the specified index from a Luau function. If no constant exists at that index, it returns nil instead.

This is useful when you want to inspect specific constant values (such as strings, numbers, or booleans) without dumping the entire list.

function debug.getconstant(func: (...any) -> (...any) | number, index: number): number | string | boolean | nil

Parameters

Parameter Description
func The Lua function (or stack level) whose constant to retrieve.
index The position of the desired constant.

Examples

Example 1

Getting a valid constant
1
2
3
4
5
6
7
local function dummy_function()
    local dummy_string = "foo bar"
    string.split(dummy_string, " ")
end

local result = debug.getconstant(dummy_function, 2)
print(result) -- Output: string

Example 2

Getting an out-of-range constant
1
2
3
4
5
6
7
local function dummy_function()
    local dummy_string = "foo bar"
    string.split(dummy_string, " ")
end

local result = debug.getconstant(dummy_function, 3)
print(result) -- Output: nil

Example 3

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