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

# getsenv

Returns the script environment of a running `LocalScript`.

```luau theme={null}
getsenv(script: LocalScript): table
```

The simplest way to picture it: `getsenv(script)` hands back the same table [`getfenv()`](https://create.roblox.com/docs/reference/engine/globals/LuaGlobals#getfenv) would return if you called it from inside that running script.

Most commonly used to read or rewrite a running `LocalScript`'s globals - spoof config flags, capture debug state, all without modifying the source.

`getsenv` only works once the script is running. A `LocalScript` that has not started executing yet has no environment to return.

The table's direct keys are the script's own globals. `GLOBAL_VAR = true` at the top of the script becomes the `GLOBAL_VAR` key; locals declared with `local` never reach the table - Luau's compiler inlines them into stack slots and upvalues.

Roblox globals (`game`, `print`, `task`, `require`) reach the table through its metatable's `__index` instead of as direct keys. For a `LocalScript` thread, `gettenv(t)` returns the same table (`rawequal` true).

## Parameters

| Parameter | Type                                                                                 | Description                                         |
| --------- | ------------------------------------------------------------------------------------ | --------------------------------------------------- |
| `script`  | [`LocalScript`](https://create.roblox.com/docs/reference/engine/classes/LocalScript) | The running LocalScript whose environment to fetch. |

## Returns

`table` - the LocalScript's global environment.

## Example

Pick a target `LocalScript` with a known boolean global (e.g. an admin script's `IS_ADMIN` flag), read its current value, flip it, observe the script's behavior change.

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  local scriptEnv = getsenv(workspace.Part.LocalScript)
  =======
  -- getsenv(script: LocalScript): table
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  local target = game:GetService("Players").LocalPlayer.PlayerScripts:FindFirstChildOfClass("LocalScript")
  local env = getsenv(target)
  print(`current IS_ADMIN: {tostring(env.IS_ADMIN)}`)
  env.IS_ADMIN = true
  print(`flipped to: {tostring(env.IS_ADMIN)}`)
  ```
</CodeGroup>

<Tip>
  For most jobs, reach for `getscriptclosure` instead. `getsenv` only shows globals the script declares on purpose, and modern scripts rarely expose any - they keep everything `local`. The closure `getscriptclosure` hands back feeds `debug.getconstants` and `debug.getprotos`, so you can inspect the strings, numbers, and inner functions the script actually runs - none of which the globals table holds.
</Tip>
