debug.getstack
Returns the stack value at a given register index.
function debug.getstack(level: number, index: number?): any | {any}Synopsis
How it works
Reads values from the Luau VMLuau VMThe Luau Virtual Machine — a register-based bytecode interpreter. Executes compiled Luau instructions sequentially, managing a value stack, call stack, and upvalue chains. Roblox runs one VM per Actor context. stack at the specified call
level. If index is given, returns the single stack slot at that position. If omitted, returns the entire stack frameCall stackThe chain of CallInfo frames representing nested function calls. Each frame records the closure, base register, top, and saved program counter. Walking the call stack reveals the full execution trace. as an array. Stack level 1 is the calling function, 2 is its caller, etc.VM internals — CallInfo
The Luau VMLuau VMThe Luau Virtual Machine — a register-based bytecode interpreter. Executes compiled Luau instructions sequentially, managing a value stack, call stack, and upvalue chains. Roblox runs one VM per Actor context. maintains a call stackCall stackThe chain of CallInfo frames representing nested function calls. Each frame records the closure, base register, top, and saved program counter. Walking the call stack reveals the full execution trace. as an array of
CallInfo structures:
typedef struct CallInfo {
StkId base; // base register pointer into L->stack
StkId func; // pointer to the function TValue
StkId top; // top of available stack for this frame
const Instruction* savedpc;
} CallInfo;
Stack slots within a frame are registers. R(0) is at ci->base + 0, R(1) at ci->base + 1, etc. The compiler assigns locals to specific registers:- Parameters:
R(0)throughR(numparams-1) - Local variables: subsequent registers in declaration order
- Temporaries: subexpressions and intermediate values
debug.getstack(level, index) reads ci->base[index - 1] (1-based to 0-based conversion).Usage
Read a stack register
local _ = "a" .. "b"
print(debug.getstack(1, 1)) "cc">--> abParameters
level number Stack frame level (1 = current).
index number optionalRegister index within the frame.
Returns
any | {any} The register value, or entire frame array if no index.