LuaStateProxy:Execute

Schedules code execution on this Lua state.

Syntax

state:Execute(source: string, ...: any) -> ()

Parameters

ParameterTypeDescription
sourcestringThe Luau code to execute
...anyArguments passed to the script

Returns

This method does not return a value.

Description

LuaStateProxy:Execute runs Luau source on the selected state and passes additional arguments through ....

Example

local state = getluastate()

state:Execute([[
    print("Executed on state:", ...)
]], "arg1", "arg2")

Execute on Actor State

local actorState = assert(getactorstates()[1], "No active actor state")
actorState:Execute([[
    print("Running on actor state!")
    print("Arguments:", ...)
]], "test", 123)

Pass Data to State

local gameState = assert(getgamestate(), "Game state is not available")

local data = {
    message = "Hello",
    value = 42
}

gameState:Execute([[
    local data = ...
    print("Message:", data.message)
    print("Value:", data.value)
]], data)

Setup Code on New States

on_actor_state_created:Connect(function(actor)
    local state = getluastate(actor)

    state:Execute([[
        -- Setup code runs before any scripts
        print("State initialized!")
        _G.setupComplete = true
    ]])
end)

Notes

  • Arguments are passed via varargs (...)
  • Useful for running setup code on another state