setfenv
Changes a function or thread environment.
Syntax
Signature
setfenv(Target: function | thread | number, Environment: table) -> (function | thread)?Aliases
This function has no aliases.
Parameters
| Parameter | Type | Description |
|---|---|---|
Target | function | thread | number | Function, thread, or nonnegative stack level. |
Environment | table | Table used as the target's environment. |
Returns
| Type | Description |
|---|---|
(function | thread)? | Selected target. Level 0 returns no values. |
Notes
Changes the environment of a Luau function, thread, or stack level. Level 0 changes the current thread's environment and returns no values. Other targets return the selected function or thread. C closures and unsupported targets raise an error.
Example
Luau
local Function = function() return Value end
setfenv(Function, {Value = 42})
assert(Function() == 42)