debug.getconstant
Returns the constant at a given index in a function's constant table.
function debug.getconstant(func: function | number, index: number): anySynopsis
How it works
Reads a constant from a Luau 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
Proto constant tableConstant tableThe Proto.k[] array containing all literal values (strings, numbers, booleans, nil) referenced by a function's bytecode. Instructions like LOADK and GETGLOBAL index into this table. at the specified 1-based index. Constants are literal values embedded in 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.: strings, numbers, booleans, and nil. When func is a number, it refers to a stack level (like debug.getinfo).VM internals
Each Luau
Proto struct contains a k[] (constants) array. Bytecode instructions like LOADK, GETGLOBAL, and NAMECALL reference constants by index. Reading these reveals the string literals, global names, and numeric values a function uses without decompilingDecompilerA tool that converts compiled bytecode back into readable Luau source code. Not a perfect inverse — variable names, comments, and some control flow structures are lost during compilation..Usage
Read constants
local function foo()
print("Hello, world!")
end
print(debug.getconstant(foo, 1)) "cc">--> "print"
print(debug.getconstant(foo, 3)) "cc">--> "Hello, world!"Parameters
func function | number A Luau function or stack level.
index number Numerical index into the constant table.
Returns
any The constant value at index.