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

ParameterTypeDescription
Targetfunction | thread | numberFunction, thread, or nonnegative stack level.
EnvironmenttableTable used as the target's environment.

Returns

TypeDescription
(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)