types.Player
A player in this instance. Calls raise once the player has left; alive checks first.
Signals
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)Fields
player.alive: booleanWhether the player is still in this instance.
player.food: numberThe hunger bar; 20 is full.
player.health: numberHealth in half-hearts; 20 is ten hearts.
player.id: stringThe player's UUID, the same in every instance and session.
player.inventory: types.PlayerInventoryThe player's inventory.
player.maxHealth: numberThe most health the player can have, in half-hearts.
player.name: stringThe player's name.
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: vectorWhere the player stands.
player.sneaking: booleanWhether the player is sneaking.
player.velocity: vectorThe 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: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
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)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)