---
title: "debug.setconstant"
description: "Changes a constant while preserving its native type."
---

```lua
debug.setconstant(target: function | number, index: number, value: any): ()
```

---

## Aliases

- `setconstant`

---

## Arguments

<ParamField body="target" type="function | number" required>
  The function to inspect, or a positive stack level identifying a caller.
</ParamField>

<ParamField body="index" type="number" required>
  A 1-based slot index.
</ParamField>

<ParamField body="value" type="any" required>
  The replacement constant; its type must match the existing slot.
</ParamField>

---

## Returns

This function returns no values.

Type mismatch raises. Function/table constants are not replaced. Compiler optimization means literal source positions are not constant indexes.

**Related types:** [Debug types & behavior](/reference/debug/types).

---

## Example

```lua
local function sample() return "docs-before" end
for index, value in pairs(debug.getconstants(sample)) do
    if value == "docs-before" then debug.setconstant(sample, index, "docs-after") end
end
print(sample())
```
