debug.getconstant

Gets a constant from a function at the specified index.

Syntax

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

Parameters

ParameterTypeDescription
funcfunction, stack level, or ProtoProxyThe prototype to inspect
indexnumberThe constant index (1-based)

Returns

TypeDescription
anyThe constant value at the specified index

Description

debug.getconstant retrieves a constant value from a function's bytecode. Constants are literal values like strings, numbers, and booleans that are embedded in the compiled function.

Example

local function example()
    local x = "hello"
    local y = 42
    print(x, y)
end

-- Constant order is compiler-dependent, so discover the index first
for index, value in ipairs(debug.getconstants(example)) do
    print(index, debug.getconstant(example, index), value)
end

Using Stack Level

You can also pass a stack level instead of a function:

local function inner()
    -- Get constant from the calling function (level 2)
    print(debug.getconstant(2, 1))
end

local function outer()
    local msg = "from outer"
    inner()
end

outer() -- Prints a constant from outer

Notes

  • Index is 1-based
  • Not all indices may have constants (some may be nil)
  • Only works with Luau closures