Saved data

Saved data is how a game remembers things between visits: coins, items, settings. A store is a named place to keep values, each under a key.

--!server
local bank = store("Bank")

world.joined:connect(function(player)
    local coins = bank:get(player.user_id) or 0
    player:tell("You have " .. coins .. " coins.")
end)

Always use player.user_id, never the player entity, as the key: the entity is gone when the player leaves, and the number is theirs for good.

What a store can do

MethodDoes
store:get(key)The saved value, or nil when nothing is saved.
store:set(key, value)Saves a value.
store:increment(key, amount?)Adds to a number (1 by default; a missing key starts at 0). Returns the new number.
store:remove(key)Removes the key. Returns what it held.
store:update(key, fn)Changes a value safely. See below.

A key is a string or a whole number. A value is a number, a string, a boolean, or a table of those (a list, or a table with text keys). Entities, vectors, functions and tables that contain themselves cannot be saved: save x, y and z separately.

bank:set(player.user_id, { coins = 50, items = { "sword", "shield" }, hardcore = true })
local save = bank:get(player.user_id)
print(save.coins, save.items[2], save.hardcore)

Waiting

A request goes to the game's saved data and the answer comes back a moment later. The task that asked waits for it, without holding anything else up: other scripts, other handlers and the physics keep running. That means saved data can be used where a task can wait: at the top of a script, inside scope:spawn, and inside any signal handler. It is an error elsewhere.

world.joined:connect(function(player)
    local save = bank:get(player.user_id)    -- this handler waits here; the game does not
    ...
end)

If the task is stopped while it waits (its scope ended), it is not woken, and the answer is dropped.

Updating safely

Two game servers can run the same place at once, so "read, change, write" can lose a change when both do it together. update does it safely: your function receives the saved value and returns the new one. If the value changed in the meantime, your function runs again on the newer value.

bank:update(player.user_id, function(save)
    save = save or { coins = 0 }
    save.coins += 10
    return save
end)

Return nil to leave the key alone. update gives up with an error after a few attempts if the value keeps changing. Keep the function short and free of waiting: it may run more than once.

For a single number, increment is simpler and just as safe.

Errors and limits

A request that fails (a bad value, a limit, the website being unreachable) raises an error in the task that asked, which you can catch with pcall:

local ok, err = pcall(function() return bank:get(player.user_id) end)
if not ok then
    warn("could not load", player.name, err)
end
WhatLimit
Store name1 to 50 characters.
Key1 to 100 characters.
One value64 KB once saved. Numbers must be finite and no larger than about 9 quadrillion.
Requests600 a minute per server, and 64 waiting at once.
TablesUp to 16 levels deep.

Games played in Polycarbonate's Play button keep saved data in memory, so you can test your scripts, but nothing survives the session.

Saves happen when you make them. There is no automatic saving of player values: save what matters when it changes, and again when the player leaves (world.left).