Skip to content

Server and clients

Every multiplayer place runs on a server. The server simulates physics, runs server scripts and owns every entity in the place. Each player's client draws the copy of the world that the server sends, runs client scripts, and sends the player's input and messages back. When the server and a client disagree, the server wins.

Server and client scripts

A script attached to an entity runs on each player's client when its file name ends in .client.luau, and on the server otherwise; see Your first script. Scripts that run for the whole place covers the folders and load order.

Some calls only make sense on one side. Calling a server-only function from a client script raises <Name> can only be called from the server, and calling a client-only one, such as local input or the camera, from a server script raises <Name> can only be called from a client script. The reference tags each member as Server only or Client only, and Engine errors lists the messages.

Change the world from the server

A server script changes the world for everyone. The engine sends each change to every client in its next network update, and a player who joins later gets the current values.

A client script may write properties of any entity, but the write stays on that client. That is useful for effects only one player should see:

-- Client script: tint the door for this player only.
local door = World.WaitFor("Door", 10)
if door then
    door.Color = Color.new(1, 0.8, 0.2)
end

The server never sees that write, and on a server-owned entity it lasts only until the server next changes the same component. A client script also cannot call Destroy, Add or Remove on a server-owned entity, or write its Parent. When a player's action needs to change the shared world, the client sends a Net.ToServer message and a server script makes the change; see Events. Network authority and replication has the complete rules, including the warning for client writes to collision properties.

How often clients get updates

The server simulates 60 ticks per second and sends what changed to each client up to 60 times per second. Each update carries only the components that changed, and a new Position goes out only when it rounds to a different 1/1024-unit step than the last one the client got.

Each player gets about 120 KB of property updates per second. When more changes than that are waiting, the server sends first the ones the player would notice most: the player's own character, entities near the player or in front of the camera, and the largest position errors. The rest go out on later ticks, so in a busy place far-away parts update less often.

Local character prediction

The client predicts one entity: the character of Players.Local, Players.Local.Avatar. On every 1/60-second physics step it applies the player's input to the character at once and sends the same input to the server, so movement shows on screen without waiting for a round trip.

When the server's result for an input arrives, the client compares it with what it predicted for that input. If the position differs by more than 0.01 units, the velocity by more than 0.1 units per second, or the rotation or movement state differs, the client takes the server's state and replays the inputs the server has not answered yet; Physics.Replaying is true during that replay. The visible jump is smoothed out over a few frames; a correction of more than 4 units snaps the character into place instead. When more than 90 inputs are unanswered, as on a stalled connection, the client takes the server's state without replaying.

A client script that writes the character's Velocity therefore does nothing lasting: the server never sees the write, and the next correction undoes it. For a dash or a wall jump that shows on the same tick on both sides, use Replicated input actions and Character abilities.

Other players and moving parts

The client does not predict anything else. It draws other players' characters and moving parts by interpolating between the updates the server sent, a short time behind the newest one, so they move smoothly but slightly behind where the server has them.

Players and characters

A player is an entity with a User component: player.User.Id is the account's user ID and player.User.DisplayName the name on its nametag. Players.All lists the players in the place, Players.Get finds one by user ID, and Players.Of gives the player that owns a character or one of its body parts. A player's character is player.Avatar, and Character is the character view of a character root, with its State and SetState.

Joins

Component.User.Added fires with each player entity that joins. A handler connected at the top of a server script sees every player who joins after the scripts start. On a client, it fires for player entities that reach the client after the handler connects; Players.All() lists the players who are already there.

-- Server script scripts/autorun/Welcome.server.luau
Component.User.Added:Connect(function(player: Entity)
    local user = player.User
    if user then
        print(user.DisplayName .. " joined with user ID " .. user.Id)
    end
end)

Spawn a character

The PlayerSpawner platform feature spawns a character for each player who joins. It places the character on a SpawnLocation of the player's team when one exists. After the character dies, it spawns a new one when the respawn delay, 5 seconds unless you change it, has passed, and destroys the old body at the same time. Without that feature, a player has no character.

Component.Character.Added fires on the server for every new character, including ones a script spawns with Prefabs.Spawn, so check Players.Of to tell a player's character from the others. A server script can move the character from that handler:

-- Server script: start each player's character above the arena.
Component.Character.Added:Connect(function(character: Entity)
    if Players.Of(character) then
        character.Position = Vector3.new(0, 20, 0)
    end
end)

Death

A character dies when it enters the "Dead" State, for example when a server script calls character.Character:SetState("Dead"). Luduvo has no built-in health or damage: a place that needs them defines its own component with World.DefineComponent and puts the character into "Dead" when its health runs out. The world never destroys a body that falls off the map, so a place with a drop handles falling characters itself.

To react to deaths, watch the state of each character:

-- Server script scripts/autorun/Deaths.server.luau
Component.Character.Added:Connect(function(character: Entity)
    character:On(Component.Character.State.Changed, function()
        local view = character.Character
        local player = Players.Of(character)
        if view and view.State == "Dead" and player then
            print(player.Name .. " died")
        end
    end)
end)

The body stays in the world. PlayerSpawner destroys it when it spawns the new character; in a place without PlayerSpawner, a server script destroys it.

Leaves and kicks

When a player leaves, the server destroys the player's character and runs the server's Component.User.Removed handlers with the player. The player entity still exists while they run, so a handler can read player.User and player.Name. Once the handlers return or first wait, the server destroys the player entity, so read what you need before a call that waits; see Save player data. Each client does the same when it removes the player: its User.Removed handlers run while the player entity still exists, and then the client destroys it. When a client disconnects from the server, it runs its User.Removed handlers for every player before it clears its world.

When the server shuts down, every player leaves, every server User.Removed handler runs, and the server waits up to 30 seconds for the CloudStore requests those handlers start.

Players.Kick(player, reason) disconnects a player at the end of the current server step and shows them the reason. The player then leaves like any other. It returns false without kicking when 64 kicks are already waiting in that step.

Stream a large world

By default every client gets every entity in the place. In a large world, turn on Enabled on the place's Streaming settings in Studio. The engine then divides the ground plane into square cells 64 units wide, and each client gets only the entities in the cells within the World settings' ViewRadius of the cell its character stands in. Height does not count. ViewRadius takes 64 to 1536 units in steps of 64, and defaults to 1024.

As the character moves, entities in cells that come into range stream in, and entities stream out once their cell is one cell past the radius, so an entity at the edge does not flicker in and out. A character's body parts always stream with its root.

On a client, an entity that has not streamed in yet does not exist. Find it with World.WaitFor or entity:WaitFor(path, seconds), or connect a ChildAdded handler with entity:On(Entity.ChildAdded, fn), instead of assuming it is there when the script starts.

How it works

The server gives every replicated entity a network ID, and clients find their copy of an entity by that ID. When a server script destroys an entity, its removal reaches the clients in the next update. The server never hands out the same ID twice in a session, so a client never mistakes a new entity for one that was destroyed. Entities a client script creates get IDs from a separate range and stay on that client.

The client sends its input, not its character's position, so the server decides where every character is. Prediction only lets the local character move before that decision arrives, which is why a correction can pull it back.