Script
The type that represents an instance of a script that is attached to a Client Control, set as the Global Script, or running as a Module.
Methods7
function Script:EnableUpdate(enabled: boolean)
Sets whether the script executes OnUpdate and OnLevelUpdate lifecycle functions.
See:
enabledbooleanWhether to execute OnUpdate and OnLevelUpdate lifecycle functions.
function Script:GetParam(varName: string) -> value any
Gets the value of a Script Variable defined in the script's mapping by name.
- Returns nil if a Script Variable with the specified name does not exist.
See:
- ServerDataType for all data types that can be returned.
varNamestringThe name of the Script Variable.
valueanyThe value of the Script Variable.
function Script:Invoke(funcName: string, ...: any) -> ...any
Calls a global function by name.
- If calling a global function from another script instance, it is recommended to check that the target script is alive.
See:
funcNamestringThe name of the function to call (case-sensitive).
...anyParameters to pass to the function call.
- ...any
The return values of the called function.
function Script:RegisterCustomVariableChangedHandler(entity: EnumItem.CustomVariableEntityType, varName: string, callback: fun(entity: EnumItem.CustomVariableEntityType, varName: string) -> nil)
Registers a Custom Variable changed handler for the specified variable name and entity.
- The variable must be set in the Custom Variable component of the entity.
- The callback function does not provide pre-change or post-change values.
- Callback functions will be called even if "Trigger Event" is false.
- Multiple handlers for the same Custom Variable cannot be registered on the same script; only the earliest handler will take effect.
Data Type Callback Behavior
Lists will call the callback function when:
- A list value is inserted.
- A list value is set.
- An exisiting list value is removed.
- The list is cleared, even if already empty.
- The list is concatenated.
- The list is sorted.
Dictionaries will call the callback function when:
- A key-value pair is set or changed.
- An exisiting key is removed.
- The dictionary is cleared, even if already empty.
Structures will call the callback function when:
- Any action that triggers a change event in lists, dictionaries, or nested structures occurs.
- The structure is modified.
Structure behaviors apply to Structure Lists and Structure Dictionaries.
See:
- Enum.CustomVariableEntityType for all entities that Custom Variables changes can be handled on.
- game.GetGlobalCustomVariableValue
entityEnumItem.CustomVariableEntityTypeThe entity to handle Custom Variable changes on.
varNamestringThe name of the Custom Variable.
callbackfun(entity: EnumItem.CustomVariableEntityType, varName: string) -> nilThe callback to execute whenever the specified Custom Variable changes.
function Script:RegisterServerSignalHandler<T>(signalName: T, callback: fun(signalName: string, signalParams: T | table) -> nil)
Registers a Client Scripted Signal handler for the specified signal name.
- Multiple handlers for the same signal cannot be registered on the same script; only the earliest handler will take effect.
- Only signals sent using the Send Client Scripted Signal server node will be handled, and all server-sent signals have a minimum latency of approximately 100ms.
- Callback parameters can only be accessed by sequence index, matching the order in the Server Signal Explorer.
To get typed signal parameters, create a class using LuaLS annotations with the same name as your signal. See below:
---@class MySignal
---@field [1] string
---@field [2] integer
---@field [3] Vector3[]
script:RegisterServerSignalHandler("MySignal", function(signalName, signalParams)
local myString = signalParams[1] -- Now typed as a string
local myInt = signalParams[2] -- Now typed as an integer
local myVec3List = signalParams[3] -- Now typed as a 3D Vector list
end)
See:
- ServerDataType for all data types that can be returned in the signalParams table.
signalNameTThe name of the signal to register a handler for.
callbackfun(signalName: string, signalParams: T | table) -> nilThe callback to execute whenever the signal is received.
function Script:UnregisterCustomVariableChangedHandler(entity: EnumItem.CustomVariableEntityType, varName: string)
Removes the handler for the specified Custom Variable.
See:
- Enum.CustomVariableEntityType for all entities that Custom Variable changes can be handled on.
entityEnumItem.CustomVariableEntityTypeThe entity to unregister the handler from.
varNamestringThe name of the Custom Variable.
function Script:UnregisterServerSignalHandler(signalName: string)
Removes the handler for the specified Client Scripted Signal.
signalNamestringThe name of the signal to unregister the active handler for.
Fields6
Script.alive : boolean
[Read] Whether the script instance is currently loaded. Check this field before calling user-defined global functions from a different script.
Script.id : number
[Read] The runtime ID of the script instance.
Script.prefabIndex : nil | number
[Read] The mapping ID of the script. Always nil for Modules.
Script.object : ClientControlType | nil
[Read] The Client Control that the script is attached to. Always nil for the Global Script and Modules.
Script.path : string
[Read] The path of the script relative to the external_lua_file directory, without the file extension.
Script.enabled : boolean
[Read/Write] The active execution state of the script. Not observed to have any effect on lifecycle functions or event handlers when changed directly.