Client scripts, messages and interface

Some things only a player's own machine can do: read the keyboard and mouse, draw a score on the screen, react at once without waiting for the server. A script that starts with --!client runs on each player's machine, and talks to the server's scripts with messages.

--!client
local label = ui.label { text = "Hello, " .. local_player.name, x = 20, y = 20, size = 28 }

Where scripts run

DirectiveRuns on
--!server (default)The game server only. Players never receive its source.
--!clientEvery player's machine.
--!sharedBoth.

What players can read. A script's source is sent to players only when it says --!client or --!shared. A server script, and anything in ServerScriptService or ServerStorage, never leaves the server. A ModuleScript object is sent to players like any shared file: keep secrets in server scripts.

Client scripts are not trusted. A player can read them, change them, or skip them. Anything that matters (health, money, who won) is decided by a server script, and the client only asks. See Messages.

What a client script has

NameIs
local_playerThe Player entity of the person playing.
worldThe world as the server last told this machine: parts, lights, sounds and the other players. Changes the server makes show up here.
inputKeys, mouse and wheel. See Input.
uiLabels and buttons on the screen. See Interface.
netMessages to and from the server.
everything elsescope, signals, tweens, queries, require, as on the server.

A client script can change parts (part.color = rgb(255, 0, 0)), and the change is drawn on this machine only. The server and other players do not see it, and when the server changes that part the server's version replaces it. Parts a client script creates exist only for this player.

Saved data (store) is for server scripts. Teleporting, chat and the other Player methods that do something to a player are the server's to call.

Input

input is a set of signals. Names are lowercase: letters ("e"), digits ("1"), "space", "enter", "escape", "tab", "left_shift", "up", "down", "left", "right", "f1" to "f12". Mouse buttons are "left", "right" and "middle".

--!client
input.key_down:connect(function(key)
    if key == "e" then
        print("pressed E at", input.cursor)
    end
end)

input.mouse_down:connect(function(button)
    print("clicked", button)
end)
MemberFires or is
input.key_down(key), input.key_up(key)A key was pressed or released.
input.mouse_down(button), input.mouse_up(button)A mouse button.
input.moved(x, y)The cursor moved (pixels from the top left).
input.wheel(ticks)The wheel turned: positive away from you.
input.cursorWhere the cursor is, as a vector (x, y).
input.is_down(name)Whether a key is held. For a mouse button, "mouse_left" and so on.

A key typed into the chat box, a click on the interface or a menu being open is not sent to your script. A key or button released always is, so nothing is left held.

Interface

ui.label and ui.button put text on the screen. Properties are checked like any other: a mistake is an error that says what to change.

--!client
local score = ui.label { text = "Score: 0", x = -20, y = 20, size = 28, anchor = "top_right" }

local buy = ui.button { text = "Buy", x = 20, y = -60, anchor = "bottom_left" }
buy.clicked:connect(function()
    print("buy clicked")
end)

score.text = "Score: 10"      -- changes the screen
score:destroy()               -- removes it
PropertyMeaningDefault
textWhat it says (at most 256 characters).empty
x, yPixels from the anchor point.0
anchorWhere on the screen the position is measured from: top_left, top, top_right, left, center, right, bottom_left, bottom, bottom_right.top_left
width, heightSize in pixels.label 200 x 26, button 120 x 36
sizeText size.18
colorText color.white
backgroundA color for a box behind the text (a button has one; set it to nil to remove it).none, button dark
transparency0 solid to 1 invisible (of the box when it has one, else the text).0
visibleWhether it is drawn.true
zHigher draws on top.0

Buttons also have clicked, a signal. ui.clear() removes everything. An interface may have at most 200 elements.

Messages

A message is a typed note between a client script and the server. You declare it once with the fields it carries, and both sides must declare it the same way. The easiest way is a --!shared script, or the same line in the server script and the client script.

--!shared
local Buy = net.message("buy", { item = "string", crate = "entity" })
local Bought = net.message("bought", { item = "string" }, { rate = 5 })

A field is a type name: "number", "string", "boolean", "vector" or "entity". Add ? to make it optional ("number?"), or write the type in braces for a list ({ "string" }, up to 64 items). A string can be at most 512 characters, and a whole message must fit in about a kilobyte.

Client to server:

--!client
Buy:send { item = "sword", crate = world.root:find("Crate") }
--!server
Buy:listen(function(player, data)
    print(player.name, "wants", data.item)
    Bought:send(player, { item = data.item })       -- to that player
    -- Bought:broadcast { item = data.item }         -- to everyone
end)

Server to client:

--!client
Bought:listen(function(data)
    print("you got", data.item)
end)
MethodOnDoes
message:send(fields)clientSends to the server.
message:send(player, fields)serverSends to one player.
message:broadcast(fields)serverSends to every player.
message:listen(fn)bothfn(player, fields) on the server, fn(fields) on a client. Returns a connection.

Checks

The sender is checked as you send: a missing field, a wrong type, a field that does not exist (with a suggestion), a dead entity. A mistake is an error and nothing is sent.

The receiver checks again, because a player can send anything. A message that is malformed, or declared on neither side, is dropped and noted as a warning in the server's Output; your listener never sees it. On the server:

  • each player may send each message at most rate times a second (30 unless you say otherwise), and the rest are dropped;
  • an entity field arrives only when it names something that exists. That does not make the request allowed: a player can name any part, so check that what they ask for is theirs to ask (is it their crate? can they afford it?). A message is a request, never an instruction.
--!server
Buy:listen(function(player, data)
    local price = prices[data.item]
    if not price then
        return                      -- not for sale: ignore it
    end
    -- ...take the money, then give the item
end)

Messages are delivered in order and are not lost.