Module
@ember/instance
The world instance this script runs in, and its lifecycle.
local instance = require("@ember/instance")Signals
Fires once before the instance closes: when it empties, or when the server shuts down. Handlers get a few seconds to finish, such as saving, before the VM is closed.
local data = require("@ember/data")
local instance = require("@ember/instance")
local players = require("@ember/players")
-- Scores live in memory during the match and go to the leaderboard at the end. `closing` fires
-- once, when the instance empties or the server shuts down, with a few seconds to finish.
local board = data.leaderboard("most_coins")
local coins: { [string]: number } = {}
players.joined:connect(function(player)
coins[player.id] = 0
end)
-- A player who leaves early is submitted as they go.
players.left:connect(function(player)
board:submit(player, coins[player.id] or 0)
coins[player.id] = nil
end)
-- Anyone still here when it closes is submitted too.
instance.closing:connect(function()
for _, player in players.all() do
board:submit(player, coins[player.id] or 0)
end
end)Fields
instance.config: { [string]: any }The world's config section from world.toml.
local instance = require("@ember/instance")
local players = require("@ember/players")
-- Tuning lives in the world's world.toml, so it changes without touching the script:
--
-- [config]
-- roundSeconds = 180
-- startingCoins = 10
--
-- Values come back untyped; annotate them and give each a default.
local ROUND_SECONDS: number = instance.config.roundSeconds or 120
local STARTING_COINS: number = instance.config.startingCoins or 0
players.joined:connect(function(player)
if STARTING_COINS > 0 then
player.inventory:add("coin", STARTING_COINS)
end
player:send(`<yellow>Rounds last {ROUND_SECONDS} seconds on {instance.map}.`)
end)instance.experience: stringThe experience this instance runs.
instance.key: string?The key that picked this copy of a keyed world.
instance.map: stringThe map currently loaded.
instance.maxPlayers: numberHow many players the world admits.
instance.world: stringThe world of the experience this instance is a copy of.