> ## Documentation Index
> Fetch the complete documentation index at: https://docsuncv2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# gettenv

Returns the global environment table of a Lua thread - for a `LocalScript` thread, the exact same table `getsenv(script)` returns.

```luau theme={null}
gettenv(t: thread): table
```

When `t` belongs to a `LocalScript`, `gettenv(t)` returns the same table as `getsenv(script)` (`rawequal` true). `gettenv(t).script` is the owning script, the same Instance `getscriptfromthread(t)` returns.

## Parameters

| Parameter | Type     | Description                                |
| --------- | -------- | ------------------------------------------ |
| `t`       | `thread` | The Lua thread whose environment to fetch. |

## Returns

`table` - the thread's global environment. For a `LocalScript` thread it is identical (`rawequal`) to `getsenv(thatScript)`: direct keys are the script's own globals, Roblox globals (`game`, `print`, `task`) resolve through the metatable. Carries a `.script` field pointing at the owning script.

## Example

In a Roblox game, confirm a thread's environment is the same table as its script's environment.

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  local threadEnv = gettenv(coroutine.running())
  =======
  -- gettenv(t: thread): table
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  -- walk the registry, match each script thread back to its env
  for _, thread in ipairs(getreg()) do
      if type(thread) == "thread" then
          local env = gettenv(thread)
          local owner = env.script

          if owner then
              print(`{owner:GetFullName()} -> {rawequal(env, getsenv(owner))}`)
          end
      end
  end
  ```
</CodeGroup>

<Tip>
  Already holding the thread's env table from `gettenv(t)`? Its `.script` field is the owning script - the same Instance `getscriptfromthread(t)` returns, without the extra call.
</Tip>
