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

Sets a captured upvalue of a function to a new value.

```luau theme={null}
debug.setupvalue(func: function | number, index: number, value: any): ()
```

New to upvalues? [Learn more here](/docs/notes/#upvalues).

<Note>
  Give it a number instead of a function and it targets the closure running at that stack level.
</Note>

Most commonly used to change a closure's captured state mid-run - for example, swapping a config table for one that skips a check.

## Parameters

| Parameter | Type                                                               | Description                                              |
| --------- | ------------------------------------------------------------------ | -------------------------------------------------------- |
| `func`    | <span style={{ whiteSpace: "nowrap" }}>`function \| number`</span> | The closure to change, or a stack level pointing at one. |
| `index`   | `number`                                                           | Which upvalue to overwrite.                              |
| `value`   | `any`                                                              | What to store in the slot.                               |

## Returns

<code title="the function does not return a value.">void</code>

## Aliases

* `setupvalue`

## Example

Jump a closure's captured counter to any value without calling it that many times.

<CodeGroup>
  ```luau Example 1 theme={null}
  debug.setupvalue(function(...) end, 1, "test")
  ```

  ```luau Example 2 theme={null}
  local count = 0

  local function ping()
      count += 1 -- count is reassigned here, so it stays a real upvalue and isn't inlined
      print(`ping #{count}`)
  end

  ping() -- ping #1
  debug.setupvalue(ping, 1, 41)
  ping() -- ping #42
  ```
</CodeGroup>

<Tip>
  Give it a number instead of a function and it targets the closure running at that stack level.
</Tip>
