Scripting
A game’s rules are written in Luau — Lua, as Roblox extended it, with
+= and friends and optional types. If you have written any JavaScript,
Python or Lua, it will look familiar within a page. This is a walk through
the ideas; the Lua reference has every call.
Where scripts live
Scripts are .luau files in the game’s scripts/ folder (subfolders are
fine). A script runs because something carries it: select an entity, and
in the Inspector press Add script and pick one. An entity can carry
several, each keeping its own state. New in Files writes a fresh script
with comments to start from.
Saving a script — ⌘S in the Script workspace — reloads it in the running game at once. If the new version has a mistake, the game carries on with the last version that worked, and the error is shown (see When it goes wrong).
The shape of a script
Make a game from the Minimal template and open scripts/mover.luau. It
is the whole idea in thirty lines:
component("Mover", { speed = 260.0 })
local M = {}
function M.start(self)
self.pulse = self.pulse or 0
end
function M.update(self, dt)
local mover = world.get(self.entity, "Mover")
local move = vec(0, 0)
if input.down("left") then move -= vec(1, 0) end
if input.down("right") then move += vec(1, 0) end
if input.down("up") then move -= vec(0, 1) end
if input.down("down") then move += vec(0, 1) end
if move ~= vec(0, 0) then
local t = world.edit(self.entity, "Transform")
t.position += vector.normalize(move) * mover.speed * dt
end
self.pulse += dt
world.edit(self.entity, "Sprite").color[4] = 0.75 + math.sin(self.pulse * 3) * 0.25
end
return M
- A script returns a table of functions, and Cinderfox calls them:
startonce (and again every time you save the file),updateevery frame withdt, the seconds since the last one. selfis this entity’s own storage — put anything in it — andself.entityis the entity the script is on.self.pulse = self.pulse or 0, not= 0: becausestartruns again on every save, theorkeeps the value the game already had.- Positions are vectors:
vec(x, y), with.xand.y, that add and multiply like numbers. They are values — writet.position = vec(3, t.position.y), nott.position.x = 3. world.getgives you a copy of a component;world.editgives you the live one, saved for you when your function returns.- Controls are asked for by name:
input.down("left"), whereleftis an action given its keys in Project Settings → Input.input.pressedis true only on the frame a key goes down.
Components of your own
component("Mover", { speed = 260.0 }) invents a component: from then on,
+ Component in the Inspector offers Mover, a scene file can place one,
and the Inspector shows its speed to edit — while the game runs, too. The
table is its default: what a new one starts with.
Declaring a component does not add it to anything; the entity has one only once you add it. And saving a script with a new default changes new ones, not ones already in the scene — so a value you tuned in the Inspector stays tuned.
Finding things, making things, removing things
- By name. Give an entity a name in the Inspector, and
world.find("player")finds it. - By kind.
world.query("Enemy")lists every entity with anEnemy. - Making one.
world.spawn({ Transform = { position = vec(100, 200) }, Sprite = { width = 32, height = 32 } })— orworld.spawn_prefab("props/enemy.ron")for a thing set up once in a file and placed many times. - Removing one.
world.despawn(e).
What you spawn belongs to the scene, so changing scene (scene.replace)
clears it away. Something that must last across scenes — the score, say —
gets the Persistent component.
When things touch
Give two entities physics bodies and colliders in the Inspector, and when they meet, both scripts hear about it:
function M.on_contact_began(self, contact)
if world.get(contact.other, "Enemy") then
world.edit(self.entity, "Ship").health -= 10
end
end
Scripts telling each other things
events.emit("player_died", { score = 120 }) reaches every script that has
an M.on_player_died(self, payload). One entity’s scripts alone:
events.send(e, "hit", payload).
Waiting, and things that glide
A function cannot simply pause, but a task can:
task.spawn(function()
world.edit(self.entity, "Text").content = "READY"
task.wait(1.5)
world.edit(self.entity, "Text").content = "GO!"
end)
A field that should glide to a value is a tween:
tween.to(e, "Sprite", "color", { 1, 1, 1, 0 }, 0.4) fades it out over 0.4
seconds. world.after(self.entity, 2, "explode") calls this script’s
M.explode in two seconds.
Sound and music
Put sounds in sounds/ and play one by name: audio.play("laser") plays
sounds/laser.wav (or .ogg, .mp3, .flac). Music loops:
audio.play("theme", { loop = true, bus = "music" }). Songs written in the
Music workspace are exported into sounds/ and played the same
way.
Keeping things between plays
storekeeps small things on the player’s device for good — a best score, a setting:store.set("best", 1200),store.get("best", 0).savekeeps a whole run, to carry on later:save.write("slot1").
Phones
A phone has no keyboard, but the on-screen controls you set in Project
Settings → Input press the same actions, so input.down("fire") works
either way. What changes is the words: input.touching() is true while the
player is using the screen, so a prompt can say “TAP FIRE” instead of “PRESS
SPACE”.
Sharing code
local Lib = require("scripts/lib/helpers.luau") at the top of a script
loads another file and gives you what it returns — for code several scripts
use. Editing the shared file reloads every script that requires it.
When it goes wrong
print(...)writes to the Output tab, named with the entity and the file that printed it.- An error marks the entity’s row in the Scene dock, and the Inspector and Output show it in full, with the line. A script that will not compile says so, and the version before it keeps running.
- A script that never finishes — a
while true do end— is stopped after ten seconds with an error, rather than freezing the editor.
The Lua reference opens with the traps most worth knowing, then lists every call.