Besides metatables, objects of types thread, function, and userdata have another table associated with them, called their environment. Like metatables, environments are regular tables and multiple objects can share the same environment.

Environments associated with userdata have no meaning for Lua. It is only a convenience feature for programmers to associate a table to a userdata.

Environments associated with threads are called global environments. They are used as the default environment for their threads and non-nested functions created by the thread (through loadfile ([filename]), loadstring (string [, chunkname]) or load (func [, chunkname])) and can be directly accessed by C code (see Pseudo-Indices).

Environments associated with C functions can be directly accessed by C code (see Pseudo-Indices). They are used as the default environment for other C functions created by the function.

Environments associated with Lua functions are used to resolve all accesses to global variables within the function (see Variables). They are used as the default environment for other Lua functions created by the function.

You can change the environment of a Lua function or the running thread by calling setfenv (f, table). You can get the environment of a Lua function or the running thread by calling getfenv (f). To manipulate the environment of other objects (userdata, C functions, other threads) you must use the C API.