Writing Lua scripts
Updated 2026-07-24
A Formula variable usually holds a one-line calculation. When the answer needs several steps, a loop or a lookup, that same variable can hold a small Lua script instead.
When to reach for Lua
roundUp(dodo.hp / 5).Covers most day-to-day math.To switch, open the variable and choose Lua script instead of Formula. Everything else stays the same: same name, same {{ }} token on the page.
Reading other variables
A script reads every other variable through a table named vars, and must end with a return. Whatever you return is what the variable becomes.
dodo.hpvars.dodo.hpreturn math.ceil(vars.dodo.hp / 5)That is standard Lua. The usual libraries are there: math.*, string.*, and helpers like ipairs and tonumber.
Returning several fields
A script can return a whole table of named fields at once. A token then reads one field at a time with a dot, exactly like a Row link variable.
return { food = (vars.level or 1) * 5, effectiveness = 98.7 }{{ tame.food }} and {{ tame.effectiveness }}vars.level or 1 is a safety net. If level has no value yet, or 1 steps in so the math still works. Guard anything that might be missing.
The Lua editor
Picking Lua script turns the plain field into a real code editor with syntax highlighting. Two controls sit above it.
Which variables a script can see
The vars table holds every variable in scope where the script lives, not every variable on the wiki.
A script can always read outward, never inward. Where a name exists twice the innermost wins, and vars.wiki.name still reaches the wiki-level value.
Keep a script on the page that uses it. Only that page runs it, only that page ships it to the browser, and it goes through ordinary page review instead of needing wiki-wide rights.
The sandbox and safety
A script always runs in the same locked-down sandbox under the same short time limit, whether on the server or in a reader’s browser. The rules never change between the two.
io, os and require are removed before your script runs.When it recomputes live
A script resolves on the server once when the page loads, so the page always arrives with a real value in place. No waiting, no flicker.
A script that reads a Reader input, directly or through another variable, also reruns in the reader browser every time that input changes. No page reload.
The tame example does this because it reads vars.level. Drag the level control and {{ tame.food }} recomputes to match.
None of this costs a page that does not use it. The Lua engine downloads only on a page that actually runs a live script.






