Experimental

An early preview: docs, the API and the dashboard may change or break.

Report a problem
Module

@ember/players

The players in this instance. Nobody is here yet while main.luau first runs; connect to joined.

local players = require("@ember/players")

Signals

players.hurting: types.Intercept<types.HurtEvent>
#
read only

Runs before a player is hurt by anything, falls and fire too; return false to refuse it, or lower amount.

local players = require("@ember/players")
local world = require("@ember/world")

-- Runs before any damage lands, so it can cancel it or soften it. No fall damage anywhere, and
-- half damage inside the "safe" region. Like every intercept, it must not yield.
local safe = world.regions.single("safe")

players.hurting:intercept(function(event)
	if event.cause == "minecraft:fall" then
		return false
	end
	if safe:contains(event.player.position) then
		event.amount = event.amount / 2
	end
	return true
end)
players.joined: types.Signal<types.Player>
#
read only

Fires when a player arrives, after their data.player document has loaded.

local players = require("@ember/players")
local world = require("@ember/world")
local items = require("@ember/items")

-- Runs for each player as they arrive, after their saved data has loaded.
players.joined:connect(function(player)
	player:setGameMode("adventure")
	player:teleport(world.spawn, world.spawnLook)
	player.inventory:clear()
	player.inventory:add(items.create("minecraft:bow"))
	player.inventory:add("minecraft:arrow", 32)
	player:send(`<yellow>Welcome, {player.name}! {players.count()} players here.`)
end)
players.left: types.Signal<types.Player>
#
read only

Fires when a player leaves. The handle still works inside the handlers and raises afterwards.

local players = require("@ember/players")

-- Per-player state lives in a table keyed by the player's id: made on `joined`, dropped on `left`,
-- so nothing about a player outlives their visit.
type State = { kills: number }
local states: { [string]: State } = {}

players.joined:connect(function(player)
	local id = player.id
	states[id] = { kills = 0 }

	-- A loop per player has to stop once they're gone. After a wait, check `alive` first: it is the
	-- one field that doesn't raise once they've left.
	task.spawn(function()
		while true do
			task.wait(30)
			local state = states[id]
			if not player.alive or not state then
				return
			end
			player:send(`<gray>Kills so far: {state.kills}`)
		end
	end)
end)

-- The handle still works inside this handler, so reading `name` is fine; afterwards it raises.
players.left:connect(function(player)
	states[player.id] = nil
	for _, other in players.all() do
		other:send(`<gray>{player.name} left the match.`)
	end
end)

Functions

players.all() -> { types.Player }
#

Returns every player in the instance, in the order they joined.

local players = require("@ember/players")
local world = require("@ember/world")

-- Send everyone back to spawn at the start of a round, healed and fed.
local function resetEveryone()
	for _, player in players.all() do
		player:teleport(world.spawn, world.spawnLook)
		player:setHealth(player.maxHealth)
		player:setFood(20)
	end
end

resetEveryone()
players.count() -> number
#

Returns how many players are in the instance.

local players = require("@ember/players")
local ui = require("@ember/ui")

-- Hold the start until two players are in, checking once a second.
task.spawn(function()
	while players.count() < 2 do
		ui.actionBar(players.all(), `<gray>Waiting for players... ({players.count()}/2)`)
		task.wait(1)
	end
	ui.title(players.all(), "<green>Match starting!")
end)
players.get(id: string) -> types.Player?
#

Finds a player in the instance by id; nil when nobody here has it.

local players = require("@ember/players")

-- Ids outlive handles: keep the id, look the player up again when you need them.
local hunterId: string? = nil

players.joined:connect(function(player)
	if hunterId == nil then
		hunterId = player.id
		player:send("<red>You are the hunter.")
	end
end)

task.delay(60, function()
	local hunter = if hunterId then players.get(hunterId) else nil
	if hunter then
		hunter:send("<red>One minute in. Keep hunting!")
	end
end)