Players and the game
A player is an entity of class Player. Scripts see everyone who is connected, and talk to them through a handful of properties, signals and methods. Server scripts (--!server, the default) are the ones that do this.
Joining and leaving
world.joined fires with the player when someone joins, and world.left when they leave. Players who joined before your script ran are not announced again, so greet the ones already here too:
local function greet(player)
player:tell("Welcome, " .. player.name .. "!")
end
world.joined:connect(greet)
for _, player in world:query { class = Player }:entities() do
greet(player)
end
world.left:connect(function(player)
world:announce(player.name .. " left the game")
end)
A player's scope ends when they leave: anything you started with player.scope stops with them.
world.joined:connect(function(player)
player.scope:every(60, function()
player:tell("You have been here a minute more.")
end)
end)
What a player has
| Property | Type | Meaning |
|---|---|---|
name | string | Their account name. |
user_id | number | Their account number. Use it, not the entity, as the key for anything you save. Read only. |
position | vector | Where their character is. Setting it teleports them. |
health | number | Hit points, 0 to 100000. Setting it to 0 kills them. |
ping | number | The round trip to their game in milliseconds, measured by the server. Read only. |
print(player.name, player.user_id, player.position, player.health)
player.health = 100 -- heal
player.position = vec(0, 50, 0)
What you can ask of a player
| Method | Does |
|---|---|
player:teleport(to) | Moves their character to a vector. |
player:tell(text) | Shows them a private chat line. |
player:kick(reason) | Removes them from the game. The reason is shown to them. |
player:teleport(vec(0, 50, 0))
player:tell("You found the secret room!")
player:kick("Please follow the rules.")
These are checked when you call them: a wrong or missing argument is an error that names it, and nothing is queued.
What players do
| Signal | Fires |
|---|---|
player.chatted(text) | when they say something in chat |
player.died() | when their health reaches zero |
part.stepped_on(player) | when their character starts touching a part |
part.clicked(player) | when they click a part, from up to 64 studs away |
Chat commands
world.joined:connect(function(player)
player.chatted:connect(function(text)
if text == "!home" then
player:teleport(vec(0, 10, 0))
end
end)
end)
Stepping on and clicking
local lava = world.root:find("Lava")
lava.stepped_on:connect(function(player)
player.health = 0
end)
local button = world.root:find("Button")
button.clicked:connect(function(player)
player:tell("Clicked!")
end)
stepped_on is for players only. part.touched fires for other parts touching it.
Dying
player.died:connect(function()
world:announce(player.name .. " fell")
end)
Everyone at once
world:announce("The round starts in ten seconds!") -- a system message to everyone
for _, player in world:query { class = Player }:entities() do
player:tell("Get ready.")
end
world:announce(text) takes at most 256 characters.
Sound
A Sound is an entity. Make one, point it at an uploaded sound, and play it:
local music = Sound.new {
asset = "sound/12", -- the id of a sound you uploaded in Polycarbonate
looped = true,
volume = 0.5,
}
music:play()
| Property | Meaning |
|---|---|
asset | Which sound: sound/<id>. |
volume | How loud, 0 to 10; 1 is normal. |
looped | Whether it starts again when it ends. |
playing | Whether it is playing now. Read only. |
sound:play() starts it from the beginning and sound:stop() stops it.
A sound that comes from somewhere
A sound inside a part is heard from the part, and fades with distance. A sound directly in the world is heard everywhere.
local pad = world.root:find("Pad")
local ding = Sound.new { asset = "sound/3", parent = pad }
pad.stepped_on:connect(function()
ding:play()
end)
A sound set to play when the game starts, in Polycarbonate, plays for everyone, including players who join later.
Moving things with physics
A part that is not anchored is moved by physics. part:push(impulse) gives it a shove:
local crate = world.root:find("Crate")
crate.anchored = false
crate:push(vec(0, 40, 0)) -- up
Setting position on a loose part moves its body there too, instead of being overwritten on the next step.
