getscriptclosure
Returns the main closure of a script instance.
function getscriptclosure(script: Script | LocalScript): functionSynopsis
How it works
Returns the top-level 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). (the "main chunk") of a script's compiled 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.. This is the outermost function that wraps all of the script's code. Once you have it, you can use
debug.getupvalues, debug.getconstants, and debug.getprotos to inspect the script's internal state without executing it.VM internals
Every Luau script compiles to a
Proto hierarchy. The "main chunk" is the root Proto, which gets wrapped in an LClosure when the script runs. getscriptclosure returns this root 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).. All local variables at the top scope are upvaluesUpvalueA variable captured from an enclosing scope. When a function references a local variable from an outer function, that variable becomes an upvalue — stored in an UpVal struct that persists even after the outer function returns. accessible via the debug library.Usage
Access script upvalues
local script = game.Players.LocalPlayer.PlayerScripts.SomeScript
local closure = getscriptclosure(script)
print(debug.getupvalues(closure))Parameters
script Script | LocalScript The script instance to retrieve the closure from.
Returns
function The main chunk closure of the script.
Alias
Also available as getscriptfunction on some executors.