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

Returns one value from a Luau function's constant table, picked by its bytecode index.

```luau theme={null}
debug.getconstant(func: function | number, index: number): boolean | nil | number | string
```

Every Luau function keeps a constant table in its compiled bytecode: the string, number and boolean literals it uses, and the names of the globals it calls. `debug.getconstant` returns the single entry at the index you ask for.

The first argument is either the function itself or a stack level. A number is treated as a level - `0` is the call running right now, higher numbers walk up the callers - and the lookup then happens on whichever function is running there.

Run `debug.getconstants` first to see the whole table and its indices, then come back here for the one you want. C closures have no constants, so confirm the target with `islclosure` before calling or it will error.

Most commonly used to pull one known constant straight out of a script's closure once `debug.getconstants` has shown you which index it sits at.

## Parameters

| Parameter | Type                 | Description                                         |
| --------- | -------------------- | --------------------------------------------------- |
| `func`    | `function \| number` | A Luau function, or a number read as a stack level. |
| `index`   | `number`             | Which constant slot to read.                        |

## Returns

`boolean | nil | number | string` - the single constant sitting at `index` in the target's constant table.

That value is whatever the compiler stored in that slot: a string or number literal, a boolean, or the name of a global the function calls.

## Aliases

* `getconstant`

## Example

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  local getconstant = debug.getconstant(function(...) end, 1)
  =======
  -- debug.getconstant(func: function | number, index: number): boolean | nil | number | string
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  local function dummy_function()
      local dummy_string = "foo bar"
      string.split(dummy_string, " ")
  end

  local result = debug.getconstant(dummy_function, 4)
  print(result) -- Output: foo bar
  ```
</CodeGroup>

<Tip>
  Constants include strings, numbers, and functions referenced in the function's scope.
</Tip>
