Sets the environment to be used by a function. If the environment has a __environment metatable, this function will error.

See also Lua library functions.


f = setfenv([f,] t) or setfenv([stackLevel,] t)


  • f - A function (function)
  • stackLevel - Level of a function in the calling stack, or 0 to set the global environment (number)
  • t - A table (table)


  • f - The input function f (function)
This function is defined in the Lua standard libraries