Returns the environment for a function (or the global environment). If the environment has a __environment metatable, that value is returned instead.

See also Lua library functions.


env = getfenv([f]) or getfenv([stackLevel])


  • f - A function (function)
  • stackLevel - Level of a function in the calling stack (number)


  • env - Table containing all variables in the function's environment, or the global environment if f or stackLevel is omitted (table)
This function is defined in the Lua standard libraries