gettenv

Gets the environment of a thread.

Syntax

gettenv(thread: thread?) -> table?

Parameters

ParameterTypeDescription
threadthread(Optional) Thread, defaults to current

Returns

TypeDescription
table?The thread's environment, or nil for a thread from another Luau state

Description

gettenv returns the environment table for the specified thread. It defaults to the current thread. A thread whose main state differs from the caller's returns nil.

Example

-- Get current thread's environment
local env = gettenv()
print(env.print) -- The print function

-- Get another thread's environment
local thread = coroutine.create(function() end)
local threadEnv = gettenv(thread)
print(threadEnv ~= nil) -- true