Your first script¶
Scripts give a place its behavior. A script is a Luau file in the project's scripts/ folder, attached to an entity such as a part. When the place runs, the script starts and can change its entity, react to events and talk to the engine's systems.
Add a script to a part¶
- Select a part in the viewport or the Outliner.
- In the Home tab, open Script and choose Server Script.
Studio creates scripts/<part name>.server.luau, attaches it to the part and opens it in the script editor. The new file has a short example that bobs the part up and down. One entity can run up to 64 scripts, and every script on it sees the same entity.
To edit scripts in another editor, see External editors.
Write the script¶
Replace the example with this script, which turns the part into lava:
-- Server script scripts/Lava.server.luau, attached to a part.
self.Color = Color.new(1, 0.3, 0)
print("lava ready at", self.Position)
self:On(Component.Collider.Touched, function(other: Entity)
local player = Players.Of(other)
local avatar = player and player.Avatar
local character = avatar and avatar.Character
if character and character.State ~= "Dead" then
character:SetState("Dead")
end
end)
Save the file, then click Play (F5). The part turns orange, the Output panel shows the print line, and walking onto the part kills your character. The player spawner gives you a new character a few seconds later.
selfis the entity the script is attached to.Touchedis a signal of the part'sCollidercomponent;self:Onruns the function each time another collider starts touching the part. Events covers signals.Players.Ofgives the player that owns a body part, ornilfor anything else, andplayer.Avataris that player's character.Characteris the character view of a character root:Stateis what the character is doing, andcharacter:SetState("Dead")kills it.
Saving a script while the place runs reloads it in place, but a reload refuses writes at the top level of a script, such as the self.Color line. Changes to this script therefore take effect the next time you click Play; Scripts, sessions and saving explains how to write scripts that reload.
Service functions such as World.Find take a dot, and methods such as self:On and character:SetState take a colon; see Calling engine functions.
Run code every frame¶
A global function named Update runs every frame with the seconds since the last frame:
-- Server script in a part: turn 90 degrees a second.
function Update(dt: number)
local rot = self.Rotation
if rot then
self.Rotation = rot * Rotation.fromAxisAngle(Vector3.YAxis, 90 * dt)
end
end
Define Update before the script's first wait, such as task.wait or WaitFor; one defined later never runs. The simulation tick and physics shows where Update and PhysicsUpdate run in each frame.
Reach other entities¶
A script finds other entities by name. World.Find looks from the top of the place, self:Find looks below the script's own entity, and a path such as "Arena/Gate" goes down several levels. Both return nil when nothing has that name, so find what you need once when the script starts and keep it in a local:
-- Server script in a part: touching the part opens the door named Door.
local door = World.Find("Door")
if door then
self:On(Component.Collider.Touched, function()
door.Visible = false
local collider = door.Collider
if collider then
collider.Enabled = false
end
end)
end
Visible hides the door, and turning off its collider's Enabled lets characters walk through. A client script may start before the server has sent an entity; it waits for one with World.WaitFor.
Server and client scripts¶
The file name decides where a script attached to an entity runs:
- A name ending in
.client.luauruns on each player's machine. Home > Script > Client Script creates one. Use client scripts for the camera, input and the player's own interface. - Any other name runs on the server, which owns the game: physics, characters, inventories and saved data. Server Script creates a
.server.luaufile.
A client script may write properties of any entity, but the change stays on that player's machine, and a client script cannot destroy or reparent an entity that the server owns. To change the game for everyone, a client script sends a Net message and a server script makes the change; see Events and Server and clients.
Two other types of script files are not attached to an entity:
- Files in
scripts/autorun/run once for the whole place, such as a round manager or a heads-up display. See Scripts that run for the whole place. - A file that ends in plain
.luauand returns a value is a module that other scripts load withrequire. See Sharing code with require.
Limits¶
Scripts run in a sandbox. They have the standard Luau libraries and the engine's globals, and no access to files or the network of the machine they run on.
One run of a script, from the moment the engine calls it until it waits or returns, may make 1,000,000 loop iterations and calls. A run past that stops with script exceeded its instruction budget, so a loop that never ends cannot freeze the game. Split long work across frames with task.wait. Engine errors lists this and the memory limit, and Scripts, sessions and saving explains when scripts start, stop and reload.