Best practices
Do not trust the client#
Anything coming through client:… can be false. A client should never be able to say "give me 500 points": it says "I finished the exercise", and the server checks.
Bind buckets to zones#
player:setBucket(n, zone) rather than player:setBucket(n). See Buckets.
Think about restarts#
restart happens while people are playing. At startup, also handle those already present:
local function welcome(player)
-- …
end
on("playerJoin", welcome)
for _, player in players.all() do
welcome(player)
end
Players already in a zone "enter" it on the next tick, with the reason zone_added: no code needed for them.
Forget those who leave#
local cooldown = {}
on("playerLeave", function(player)
cooldown[player.id] = nil
end)
Numbers are reused: without this, a new player inherits the old one's data.
Prefer zones to loops#
The server already knows who enters and leaves, at almost no cost. A loop over every player on every tick is expensive with 512 of them.
Split the work#
Time is counted per tick, all calls included. Split a long computation with wait(0):
thread(function()
for i, item in bigList do
process(item)
if i % 100 == 0 then
wait(0)
end
end
end)
Name your zones#
The name shows up in errors and in the console.