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

Reads the values a running function has on the Lua stack.

```luau theme={null}
debug.getstack(level: number, index: number?): any | {any}
```

Every running function keeps its working values in numbered slots. `debug.getstack` hands you those slots for one function on the call chain.

`level` chooses which function: `1` is the one that called `debug.getstack`. `2` is the function that called that one, and so on up the chain.

`index` chooses one slot. Leave it out to get the whole set back as a table.

Slots are not only the variables you named. The Luau compiler adds its own values too, so slot `1` is not always your first variable.

Most commonly used while reversing a game script. You hook a function, then read the caller's slots to see the values it was about to pass in before the real call runs.

## Parameters

| Parameter | Type      | Description                                                                      |
| --------- | --------- | -------------------------------------------------------------------------------- |
| `level`   | `number`  | Which function on the call chain to read. `1` is the caller of `debug.getstack`. |
| `index`   | `number?` | Which slot to return. Leave out to get the whole set.                            |

## Returns

`any | {any}` - the shape depends on whether you pass `index`.

| Call             | Returns                                                                                |
| ---------------- | -------------------------------------------------------------------------------------- |
| `index` left out | `{any}` - a table of every slot value: your variables and the compiler's, mixed types. |
| `index` given    | `any` - the one value in that slot.                                                    |

A slot that has no value throws an error. It does not come back as `nil`.

## Aliases

* `getstack`

## Example

Read a function's whole frame. `peek` is called from `run`, so `level` `2` is `run` - its locals are still live while `peek` runs.

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  local getstack = debug.getstack(8, 1)
  =======
  -- debug.getstack(level: number, index: number?): any | {any}
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  local function peek()
      for slot, value in pairs(debug.getstack(2)) do
          print(slot, value)
      end
  end

  local function run()
      local damage = 50
      local target = "Player1"
      peek()
  end

  run()
  ```
</CodeGroup>

<Tip>
  Levels start at 1 (current function) and go up the call stack.
</Tip>
