@ember/data
Data that outlives the instance: each player's document and the experience's leaderboards.
local data = require("@ember/data")Functions
data.leaderboard(name: string, options: types.LeaderboardOptions?) -> types.LeaderboardOpens the leaderboard with the name, shared by every instance of the experience; it keeps each player's best score.
local data = require("@ember/data")
local players = require("@ember/players")
local world = require("@ember/world")
-- A parkour best-times board, shared by every instance. "asc" ranks the lowest time first.
local times = data.leaderboard("parkour", { order = "asc" })
local startedAt: { [string]: number } = {}
world.regions.single("start").left:connect(function(player)
startedAt[player.id] = task.now()
end)
world.regions.single("finish").entered:connect(function(player)
local started = startedAt[player.id]
if not started then
return
end
startedAt[player.id] = nil
local seconds = task.now() - started
-- Only kept if it beats the player's own best.
times:submit(player, seconds)
player:send(string.format("<green>Finished in %.2fs", seconds))
-- Show the top three.
for _, entry in times:top(3) do
player:send(string.format("<gray>#%d %s: %.2fs", entry.rank, entry.name, entry.score))
end
end)
players.joined:connect(function(player)
local best = times:rank(player)
if best then
player:send(string.format("<yellow>Your best: %.2fs (#%d)", best.score, best.rank))
end
end)data.player<T>(defaults: T, migration: data.Migration?) -> data.PlayerDocuments<T>Declares each player's document, whose fields and their types are the defaults'. Saved fields the defaults do not have are hidden from the script and written back with each save, and new ones start at their default, in nested tables with string keys too; any other table, such as an empty one or a list, is taken whole from the save. Renames, reshapes and dropping a field need a migration. A player whose document was saved at a higher version than the migration's is kicked before players.joined and the document is left as it was. A document saves to at most 1 MB of JSON. Only while main.luau first runs, and once.
local data = require("@ember/data")
local players = require("@ember/players")
local world = require("@ember/world")
-- Each player's saved document. The defaults give its fields and their types; call this once,
-- at the top of main.luau.
local profile = data.player({ coins = 0, wins = 0, unlocked = { hat = false } })
-- The document has loaded by the time `joined` fires.
players.joined:connect(function(player)
local saved = profile:get(player)
player:send(`<gold>You have {saved.coins} coins and {saved.wins} wins.`)
end)
-- Change it like a plain table; it saves when they leave and every minute while it changes.
world.regions.single("finish").entered:connect(function(player)
local saved = profile:get(player)
saved.wins += 1
saved.coins += 50
if saved.wins == 10 then
saved.unlocked.hat = true
player:send("<light_purple>10 wins! You unlocked the hat.")
end
end)