ReferenceEnvironment

getfenv

Reads a function's environment table using the engine's standard getfenv behavior.

Syntax

getfenv(target: function | number?) -> table

Parameters

ParameterTypeDescription
targetfunction or number?Function or caller-stack level; defaults to the calling function.

Returns

TypeDescription
tableEnvironment associated with the selected function or caller.

Description

This can be a script-local table rather than the shared table returned by getgenv. Reading an environment does not copy it: changing the returned table changes the values that function resolves through it. Invalid arguments and unavailable caller levels raise an error. Exposing an environment clears its optimized-import safety flag, as the native Luau function does; subsequent changes to builtins must be observed by ordinary lookups.

Example

local function example() return 5 end
local environment = getfenv(example)
print(type(environment))

These are Game executor contracts. Check build requirements and session lifetimes before using them.