debug.setstack

Sets a value on the stack at a specific level and index.

Syntax

debug.setstack(level: number, index: number, value: any) -> ()
debug.setstack(thread: thread, level: number, index: number, value: any) -> ()

Parameters

ParameterTypeDescription
levelnumberThe stack level (1 = current function)
indexnumberThe stack slot index
valueanyThe new value to set
threadthread?Optional thread to modify before level

Returns

This function does not return a value.

Description

debug.setstack modifies a value on the call stack. This can change local variables in any active function on the stack.

Example

local function modifier()
    for index, stackValue in ipairs(debug.getstack(2)) do
        if stackValue == "original" then
            debug.setstack(2, index, "modified!")
            return
        end
    end
end

local function example()
    local value = "original"
    modifier()
    print(value) -- "modified!"
end

example()

Caution

Modifying stack values can cause crashes or undefined behavior if:

  • You set an incompatible type
  • The index doesn't exist
  • The value is used in unexpected ways