getsenv
Returns the global environment table of a specific Script instance.
function getsenv(script: Script | LocalScript): tableSynopsis
How it works
Locates the running thread associated with the given script instance and returns its environment table (
_ENV). This is the table where all of the script's top-level local variables are unresolvable but its global variables live. You get full read/write access to the script's global scope.VM internals
Each Luau thread's main 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). has an environment (
env) table. For game scripts, this is initially set to the Roblox global environmentGlobal environment (genv)The executor's global environment table — the _G-equivalent for executor-loaded scripts. Functions set here (via getgenv()) are accessible from any executor script but invisible to game scripts. but can be customized by setfenv. getsenv finds the script's main thread and returns the environment of its top-level closure.Caution
Writing to a script's environment can cause undefined behavior if the script reads its globals with cached references. Many game scripts capture important values in local variables at load time, so modifying the environment may not affect already-running code.
Usage
Read a game script global
local env = getsenv(game.Players.LocalPlayer.PlayerScripts.SomeScript)
print(env.someGlobalVariable)Parameters
script Script | LocalScript The script whose environment to retrieve.
Returns
table The script's global environment table.
Write Access
Writing to a script environment directly can cause undefined behaviour. Use with care.