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

Checks whether a number is a valid stack level for the thread that calls it.

```luau theme={null}
debug.isvalidlevel(level: number): boolean
```

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

It tracks `debug.getinfo` exactly: a level this returns `true` for is one `debug.getinfo` can read, and a level it returns `false` for is one `debug.getinfo` errors on.

## Parameters

| Parameter | Type     | Description                |
| --------- | -------- | -------------------------- |
| `level`   | `number` | Which stack frame to test. |

## Returns

`boolean` - `true` for a level the calling thread currently has on its stack, `false` for one it does not.

## Aliases

* `isvalidlevel`
* `validlevel`

## Example

Measure how deep the current call stack is by counting levels until `debug.isvalidlevel` runs off the top.

<CodeGroup>
  ```luau Example 1 theme={null}
  <<<<<<< HEAD
  local isvalidlevel = debug.isvalidlevel(8)
  =======
  -- debug.isvalidlevel(level: number): boolean
  >>>>>>> 50e30f53759b0538759af25c9b271514aa2c9290
  ```

  ```luau Example 2 theme={null}
  local function stackDepth()
      local level = 0
      while debug.isvalidlevel(level) do
          level += 1
      end
      return level
  end

  local function inner()
      print(`inner: {stackDepth()} levels`)
  end

  print(`top level: {stackDepth()} levels`)
  inner()
  -- inner() prints the higher number: it runs one call deeper, so
  -- isvalidlevel stays true for more levels before it runs off the top
  ```
</CodeGroup>

<Tip>
  Useful for iterating up the stack without causing stack level out-of-range errors.
</Tip>
