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

# debug.setstack

Writes a value into a single local slot of a live stack frame on the current thread.

```luau theme={null}
debug.setstack(level: number, index: number, value: any): ()
```

New to stack levels? [Learn more here](/docs/notes/#stack-levels).

Most commonly used from a hook running inside a game script's thread, to flip one of that script's locals or rewrite a return slot before the frame returns.

`value` has to match the type already in the slot. There is no coercion.

The call only reaches frames on the thread it runs on. The target script also has to be running, or the access fails.

## Parameters

| Parameter | Type     | Description                                                    |
| --------- | -------- | -------------------------------------------------------------- |
| `level`   | `number` | The stack frame to write into, counted outward from this call. |
| `index`   | `number` | The slot within that frame's local window.                     |
| `value`   | `any`    | The replacement written into the slot.                         |

## Returns

<code title="the function does not return a value.">void</code> - the slot is overwritten in place.

## Aliases

* `setstack`

## Example

A local starts `false` in one frame. A deeper call overwrites that frame's first slot, so the frame returns `true` instead.

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  debug.setstack(8, 1, "test")
  =======
  -- debug.setstack(level: number, index: number, value: any): ()
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  local function isAllowed()
      local allowed = false -- first slot of this frame

      local function flip()
          debug.setstack(2, 1, true) -- level 2 = isAllowed's frame, slot 1 = allowed
      end

      flip()
      return allowed
  end

  print(isAllowed()) --> true
  ```
</CodeGroup>

<Tip>
  Read the slot with `debug.getstack` first - it confirms the index and the current type, so the write does not trip the type check.
</Tip>
