Client scripts
A server plugin can bring Luau that runs in the players' clients: a HUD of its own, effects, view model animations, values sent to and from the plugin, and a movement script that runs the same on the server and in the client's prediction. The server can also put widgets on players' HUDs without any client script (cs.hud_widget).
Files
The scripts are the .luau files of the plugin's content/scripts/<plugin>/ folder:
plugins/jumps/
plugin.toml
main.luau the server side
content/scripts/jumps/
client.luau runs in every player's client
move.luau runs in the player movement, both sides
hud.luau a module (require("hud"))
They are precached when the map starts and downloaded by the players like the rest of the plugin's content. The server also tells each player the SHA-256 of every file, and the client checks it again before it runs a file. A file whose bytes differ is refused, and so is any script that needs it. A plugin loaded during a map brings its scripts with the next map.
The sandbox
Each plugin's client scripts run in their own Luau VM with Luau's safe libraries (no files, no network, no loadstring). The libraries, cl and the strings' metatable are read-only. require only finds the plugin's own scripts, named from the top of the scripts folder (require("lib/x")).
There are limits:
- Time: a handler call may take 4 ms. A script that fails 10 times is stopped.
- Memory: a VM may hold 32 MB.
- Drawing: 4096 drawings a frame, across all scripts.
- Messages to the server: a value sent to the server may be 2 KB packed, and the server takes 40 pieces a second from a player.
The client API (cl)
| Function | What it does |
|---|---|
cl.on("frame", fn(dt)) | Every frame. |
cl.on("draw", fn(width, height)) | Every frame, to draw the HUD (see below). |
cl.on_message(channel, fn(value)) | Values the plugin sends with cs.send_client(player, channel, value). |
cl.send(channel, value) | A value to the plugin's cs.on_client(channel, fn(player, value)). |
cl.time() | The client's clock in seconds. |
cl.screen() | The HUD's width and height (1080). |
cl.me() | The local player: slot, alive, team, health, origin, velocity, angles. |
cl.world_to_screen(pos) | Where a point of the world is on the HUD, or nothing when it is behind the view. |
cl.rgba(r, g, b, a?) | A colour as a number (0xRRGGBBAA). |
cl.sound(name, volume?) | Plays a game sound for the player only. |
cl.weapon_anim(sequence, body?) | Plays an animation of the view model. |
Values cross between the VMs as copies: nil, booleans, numbers, strings, vectors, and tables of those, nested at most 16 deep. Functions do not cross.
Drawing
These calls work only in a draw handler. They use HUD units: the screen is 1080 high and as wide as its aspect makes it. A colour is "#rrggbb", "#rrggbbaa" or a number (cl.rgba).
| Function | What it draws |
|---|---|
cl.draw.rect(x, y, w, h, color) | A filled rectangle. |
cl.draw.frame(x, y, w, h, color, width?) | A rectangle's outline. |
cl.draw.line(x1, y1, x2, y2, color, width?) | A line. |
cl.draw.circle(x, y, radius, color, width?) | A circle, filled when width is absent or 0. |
cl.draw.text(x, y, text, color, size?, align?) | Text with its top at y; align is left, center or right of x. |
cl.draw.image(path, x, y, w, h, color?, options?) | A PNG or JPG of the game's or the plugin's content, tinted (see below). |
An image's options (a table, as the 6th or 7th argument; as the 6th its color field is the tint):
| Option | What it does |
|---|---|
anchor | The picture's point placed at x, y: top_left (absent), top, top_right, left, center, right, bottom_left, bottom, bottom_right. Rotation turns about it too. |
fit | How the picture fills w by h: stretch (absent), contain (all of it, its shape kept, centred), cover (fills it, its shape kept, the rest cut off). |
slice | 9-slice margins in the picture's pixels, one number or { left, top, right, bottom }: the corners keep their size (in HUD units), the edges and the middle stretch. For frames and panels; fit does not apply. |
frames | A sprite sheet: a number of cells in one row, or { columns, rows }, read left to right then down. count cells are used (all when absent), shown at fps a second (10 when absent; 0: the first), or the one frame says (from 1). |
rotation | Degrees, clockwise. |
filter | smooth (absent) or sharp (nearest pixels: pixel art). |
alpha | 0 to 1, times the colour's own. |
HUD units scale with the screen's height, so a picture keeps its size and place on every resolution; use cl.screen()'s width (or anchor with x at 0 or the width) to stay at an edge on wide screens. A missing picture, or one that is not a PNG or JPG, draws nothing and is told once in the console.
-- a 9-sliced panel and an animated 8-cell icon in its corner
cl.draw.image("myplugin/panel.png", 40, 40, 400, 160, { slice = 12 })
cl.draw.image("myplugin/coin.png", 420, 60, 48, 48, { frames = 8, fps = 12, anchor = "top_right" })
Effects
Sprites must be precached by the plugin (cs.precache in map_start).
| Function | What it makes |
|---|---|
cl.fx.sparks(pos) | Sparks. |
cl.fx.light(pos, radius, color, life, decay?) | A dynamic light. |
cl.fx.sprite(pos, sprite, scale?, alpha?) | An animated sprite. |
cl.fx.glow(pos, sprite, scale, life, alpha) | A glowing sprite that fades. |
cl.fx.explosion(pos, sprite, scale, framerate) | An explosion sprite. |
cl.fx.beam(from, to, sprite, life, width, color) | A beam between two points. |
cl.fx.splash(pos, dir, color, count, speed) | A streak splash. |
-- content/scripts/jumps/client.luau
local jumps = 0
cl.on_message("jumps", function(n)
jumps = n
cl.fx.sparks(cl.me().origin)
end)
cl.on("draw", function(w, h)
cl.draw.text(w / 2, h - 120, "Double jumps: " .. jumps, "#ffd040", 28, "center")
end)
Movement scripts (move.luau)
move.luau runs inside the player movement, on the server for every command and in the client's prediction, so a mod's own movement is predicted without lag. Both sides run the same file (the client checks its hash), and the script must give the same numbers on both.
To keep the two sides equal:
-
Math: the math library keeps only functions that are exactly rounded everywhere.
sin,cos,tan, their inverses,exp,log,pow,randomandnoiseare gone, and so areosandvector.angle. -
Per-player state: keep it in
m.state, never in the script's own variables. The prediction replays commands, and onlym.statefollows it.
move.state({ "jumped" }) -- names of the numbers kept per player (at most 8)
move.on("before", function(m) end) -- before the game's movement
move.on("after", function(m) end) -- after it
The move m has these fields, read-only unless marked:
-
Position and motion:
origin(writable),velocity(writable),base_velocity,angles. -
Input:
buttons,old_buttons(bits frommove.buttons:jump,duck,attack...),forward_move,side_move,up_move. -
Player state:
flags,on_ground,ducking,in_duck,water_level,movetype,dead. -
Movement settings:
max_speed,gravity,friction,fall_velocity(all writable). -
The run:
frametime,player,server(true on the server),first(the first time this command runs; prediction replays it). -
What the movement hit:
touches, the normals of the planes the game's movement ran into during this command (inafter; empty inbefore). -
Kept numbers:
state.
It has two methods:
-
m:trace(from, to): moves the player's box from one point to the other and returnsfraction,endpos,normal,hit,start_solidandall_solid. -
m:sound(name, volume?): plays a sound of the player's, on the first run only.
The plugin sets a player's numbers from the server with player:set_move_state(name, value) (and reads them with player:move_state(name)): the script sees the new value from the next command, on both sides, which makes it a per-player setting of the script (on or off, a strength).
The state numbers travel in the player's physics info (key ms), so they stay small: at most 8 numbers per script, and about 120 characters for all scripts together. A player whose numbers are all 0 has no key.
-- content/scripts/jumps/move.luau: a double jump
move.state({ "jumped" })
local JUMP = move.buttons.jump
move.on("before", function(m)
if m.on_ground then
m.state.jumped = 0
return
end
if bit32.band(m.buttons, JUMP) ~= 0 and bit32.band(m.old_buttons, JUMP) == 0 and m.state.jumped == 0 then
m.state.jumped = 1
m.velocity = vector.create(m.velocity.x, m.velocity.y, 268.328157)
end
end)
The server side
cs.send_client(player, "jumps", 3) -- to one player, a list, or everyone (nil)
cs.on_client("hello", function(player, value) end)
cs.hud_widget(player, "hp", { kind = "bar", x = 0.5, y = 0.9, align = "center", value = 50, max = 100, label = "HP" })
cs.hud_remove(player, "hp") -- "": all of the plugin's widgets
Widgets are tables with a kind:
| Kind | Fields |
|---|---|
text | text, size |
bar | w, h, value, max, back, label |
icon | image (a PNG or JPG), w, h |
image | image, w, h and the image options: anchor, fit, slice, frames, count, frame, fps, rotation, filter, alpha |
list | title, items, size |
timer | seconds, up, label, size |
radar | size, range, dots ({ pos = vector, color = ... }) |
All kinds also take x and y (fractions of the screen), align and color. The client's own widgets script draws them (with the scripts' drawing).
Messages to a player are queued and sent as the reliable channel has room (about a kilobyte a frame); a single value may be 16 KB packed.
Checked by tools/plugin_client_test.sh.