Experimental

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

Report a problem

types.Player

A player in this instance. Calls raise once the player has left; alive checks first.

Signals

player.died: types.Signal<>
#
read only

Fires when the player dies.

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

-- Each player has their own `died` and `respawned` signals.
players.joined:connect(function(player)
	player.died:connect(function()
		for _, other in players.all() do
			other:send(`<red>{player.name} was eliminated.`)
		end
	end)
	player.respawned:connect(function()
		player:setGameMode("spectator")
		player:teleport(world.spawn)
	end)
end)
player.respawned: types.Signal<>
#
read only

Fires when the player comes back from the death screen.

Fields

player.alive: boolean
#
read only

Whether the player is still in this instance.

player.food: number
#
read only

The hunger bar; 20 is full.

player.gameMode: types.GameMode
#
read only

The player's game mode.

player.health: number
#
read only

Health in half-hearts; 20 is ten hearts.

player.id: string
#
read only

The player's UUID, the same in every instance and session.

player.inventory: types.PlayerInventory
#
read only

The player's inventory.

player.look: types.Look
#
read only

Which way the player faces.

player.maxHealth: number
#
read only

The most health the player can have, in half-hearts.

player.name: string
#
read only

The player's name.

player.playtime: types.Playtime
#
read only

The player's time in this experience, counted up to now; read it again for a later count.

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

-- Greet first-timers and regulars differently. `visits` counts the visits they finished before
-- this one, so it is 0 on their first.
players.joined:connect(function(player)
	local time = player.playtime
	if time.visits == 0 then
		player:send("<gold>Welcome! This is your first visit.")
	elseif time.lastPlayedAt then
		local days = math.floor((os.time() - time.lastPlayedAt) / 86400)
		player:send(`<gold>Welcome back! Visit {time.visits + 1}, {days} days since your last.`)
	end
end)

-- `playtime` is counted when you read it, so read it again for a later count.
-- `seconds` covers every visit, this one included; `sessionSeconds` only this one.
task.spawn(function()
	while true do
		task.wait(60)
		for _, player in players.all() do
			local time = player.playtime
			local hours = math.floor(time.seconds / 3600)
			local minutes = math.floor(time.sessionSeconds / 60)
			player:send(`<gray>{minutes} minutes this visit, {hours} hours in all.`)
		end
	end
end)
player.position: vector
#
read only

Where the player stands.

player.sneaking: boolean
#
read only

Whether the player is sneaking.

player.velocity: vector
#
read only

The player's velocity in blocks per second.

Methods

player:kick(reason: types.MiniMessage?)
#

Disconnects the player from the server.

player:send(text: types.MiniMessage)
#

Sends the player a chat message.

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

-- A chat message to one player, in MiniMessage.
players.joined:connect(function(player)
	player:send(`<yellow>Hi <bold>{player.name}</bold>!</yellow> <gray>Type /leave to go back.`)
end)
player:setFood(food: number)
#

Sets the hunger bar; 20 is full.

Range: food: 0..20

player:setGameMode(mode: types.GameMode)
#

Sets the player's game mode.

player:setHealth(health: number)
#

Sets health in half-hearts, capped at maxHealth; 0 kills.

Range: health >= 0

player:setMaxHealth(health: number)
#

Sets the most health the player can have, in half-hearts.

Range: health: 1..1024

player:setRespawn(position: vector, look: types.Look?)
#

Sets where the player comes back after dying, facing look or the way the last point faced; until set again it is the world spawn.

player:setVelocity(velocity: vector)
#

Sets the player's velocity in blocks per second.

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

-- A launch pad: stepping on it throws the player up and forward. Velocity is in blocks per second.
world.regions.single("launch_pad").entered:connect(function(player)
	local yaw = math.rad(player.look.yaw)
	local forward = vector.create(-math.sin(yaw), 0, math.cos(yaw))
	player:setVelocity(forward * 15 + vector.create(0, 20, 0))
end)
player:teleport(to: vector, look: types.Look?)
#

Moves the player within this world, keeping their look unless one is given.

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

-- Teleport keeps the player's look unless you pass one.
players.joined:connect(function(player)
	local lobby = world.markers.single("lobby")
	player:teleport(lobby.position, { yaw = lobby.yaw or 0, pitch = 0 })
end)
player:travelHome()
#

Sends the player back to the lobby. An instance closes once its last player has left.

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

-- The first player to reach the finish wins; then everyone goes back to the lobby. The instance
-- closes once its last player has left.
local finished = false

world.regions.single("finish").entered:connect(function(winner)
	if finished then
		return
	end
	finished = true
	ui.title(players.all(), `<gold>{winner.name} wins!`, { stay = 4 })
	-- Handlers are tasks, so this one can wait for the title to finish.
	task.wait(5)
	for _, player in players.all() do
		player:travelHome()
	end
end)