Returns the global environment table of a Luau thread. With no argument, returns the globals of the calling thread.

Parameters

thread
default:"the calling thread"
The thread whose global environment to read. Omitted, it defaults to the thread that called gettenv.

Returns

table or nil
The thread’s global environment table, or nil if the thread cannot be read from the current Lua state (see below).

Behavior

  • Same-VM only. A thread’s globals can only be read from within the same Lua state. If the target thread belongs to a different VM, such as an Actor state, gettenv returns nil rather than reaching across VMs. This is a safety boundary, not a failure to look hard enough.
  • Self short-circuit. Passing the calling thread (or no argument) returns that thread’s globals directly.
  • Per-thread globals. The table returned is the thread’s own globals index. A sandboxed thread has its own globals layered over the shared environment, so what you get back reflects that thread’s view, including any per-thread overrides.
gettenv reads the global environment of a thread. If instead you want the script backing a thread, or the thread backing a script, that lives in the scripts library.

Example

Read a thread’s globals, guarding against one from another VM:

Errors

gettenv does not raise. A thread it cannot read yields nil.