Want a special badge and 30 bux? Make some PolyWorld! Content and post it in the Creations forum!
Dark mode is incomplete! Please report any issues to the Discord.

Script

Scripts Not creatable

Scripts run Lua on the server for a place.
Each Script is a coroutine. Scripts in the same place share world globals. For example, Score = 1 in one Script is visible in others.
Per-script values such as script stay private.

The global script refers to the running Script.

Listen to world events with `Event:Connect` on objects such as Part and Players.


Overview

Properties

Events

Prefer Connect on the objects that own other signals:

EventWhere
Touched`Part.Touched`
PlayerAdded`Players.PlayerAdded`
PlayerRemoving`Players.PlayerRemoving`
PlayerDied`Players.PlayerDied`
CharacterAdded`Player.CharacterAdded`

API

Properties

Parent

Parent : Instance?
The Instance this Script is attached to, usually a Part.
nil when the Script is not parented under a Part.

parent

parent deprecated : Instance?
Identical to Parent.
No longer recommended for use.

Events

OnRemoteEvent

OnRemoteEvent : Event (name: string, playerId: int, data: table) deprecated
Triggers when a client fires a remote, for example via LocalScript FireServer.
Prefer `MessageModule.MessageFromClient`.
data is a table. For payloads of at least 24 bytes (6 floats), these fields are set:

FieldMeaning
dir_x, dir_y, dir_zAim direction
origin_x, origin_y, origin_zAim origin
hit_x, hit_y, hit_zClick / raycast hit (when payload ≥ 36 bytes)
data_lenRaw payload length in bytes

Equipping a custom Tool and clicking fires a remote whose name is the tool’s name, with the aim payload above.

script.OnRemoteEvent:Connect(function(name, playerId, data)
    if name == "BlastRadius" and data.hit_x then
        print("Click", data.hit_x, data.hit_y, data.hit_z)
    end
end)

See examples/tools/BlastRadius/ for a full 5-stud AOE damage example.

Defining function OnRemoteEvent(name, playerId, data) in a Script body is still supported.
No longer recommended for use.

Older global handlers

These Script-body globals are still supported for compatibility.
Prefer Connect on the Events above.
No longer recommended for use.

Touched

Touched(player: Player) deprecated
Older global handler for when a player enters the Script's parent Part.
Prefer `Part.Touched:Connect`.
Also sets the env global player for older scripts that call player:Kill() with no argument.
Do not call wait() inside this handler.

function Touched(player)
    player:Kill()
end

PlayerAdded

PlayerAdded(player: Player) deprecated
Prefer `Players.PlayerAdded:Connect`.

PlayerDied

PlayerDied(player: Player) deprecated
Prefer `Players.PlayerDied:Connect`.

CharacterAdded

CharacterAdded(player: Player) deprecated
Prefer `Player.CharacterAdded:Connect`.