> ## 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.

# getrenv

Returns the global environment that the **Roblox** game uses - separate from `getgenv()`,<br />which shows executor-exclusive environment.

```luau theme={null}
getrenv(): table
```

The returned table holds `_G` and `shared` directly. Pulls every other global (`print`, `game`, `task`, `require`) via using a metatable.

Almost all globals are the same closure your script sees - `getrenv().print == print` is `true`. The exceptions are `require`, `debug`, and `loadstring` - Seliware's copy and the game's copy are different functions for these three.

Using `getrenv().require` catches the game's calls and leaves yours alone.

<Tip>
  When dealing with running ModuleScripts, avoid the global `require()`, and instead use `getrenv().require()` to avoid leaking the script's environment.

  Don't forget to `setidentity(2)` first!

  ```luau theme={null}
  setidentity(2)
  local RobloxScript = getrenv().require(path.To.ModuleScript)
  setidentity(8)
  ```
</Tip>

## Returns

| Key      | Type    | Description                                 |
| -------- | ------- | ------------------------------------------- |
| `_G`     | `table` | Shared cross-script global table            |
| `shared` | `table` | Same role as `_G` with a separate namespace |

## Example

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  local robloxEnv = getrenv()
  =======
  -- getrenv(): table
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  setidentity(2)
  local RobloxScript = getrenv().require(path.To.ModuleScript)
  setidentity(8)
  ```
</CodeGroup>

<Tip>
  When dealing with running ModuleScripts, avoid the global `require()`, and instead use `getrenv().require()` to avoid leaking the script's environment.

  Don't forget to `setidentity(2)` first!

  ```luau theme={null}
  setidentity(2)
  local RobloxScript = getrenv().require(path.To.ModuleScript)
  setidentity(8)
  ```
</Tip>
