Players and characters
Players joining and leaving
players.joined:connect(function(player)
print(player.name .. " joined")
end)
players.left:connect(function(player)
print(player.name .. " left")
end)
for _, player in players.all() do -- everyone in the game now
print(player.displayName)
endIn a ClientScript, players.me is the player whose device it runs on.
A player
| Member | What it is |
|---|---|
name, displayName | Their username, and the name shown |
userId | Their account number; -1 for a guest |
health, maxHealth | Health (100 to start). Set health = 0 to kill |
walkSpeed | Units a second (20 to start, 0-200) |
jumpPower | How hard they jump (50 to start, 0-200) |
position | Where they are; set it from a Script to teleport |
team, neutral | Their team |
stats | Their stats in the player list (below) |
backpack | Their tools |
character | Their body in the world (below) |
gui | Their copy of the UI (ClientScripts) |
characterModel | A Model to draw them as instead of KHIM's body (below) |
died | Event: they died |
teleport(position) | Moves them there |
damage(amount) | Takes health |
kick(reason) | Sends them out of the game with a message (server) |
equipTool(tool), unequipTools() | Puts a tool in their hand, or away |
players.joined:connect(function(player)
player.walkSpeed = 30
player.died:connect(function()
chat.send(player.displayName .. " fell!")
end)
end)Set movement from a Script
walkSpeed and jumpPower belong to the server: the server moves every character and checks movement against its own values. Set them from a Script; a ClientScript's change wouldn't stick.
The player list: stats
player.stats puts numbers (or text) next to the player's name in the player list (Tab):
players.joined:connect(function(player)
player.stats.Coins = 0
player.stats.Level = 1
end)
-- later, on the server
player.stats.Coins += 10
print(player.stats.Coins)
player.stats.Level = nil -- takes it off- The first stat ranks the list, highest first.
- Big numbers are shortened: 12.3K, 807K, 34.5M, 1.2B.
- Stats are kept by server Scripts. They are not saved by themselves when the player leaves - see Saving data.
The character
player.character is a Model named after the player with two things in it:
character.body- health and movement:health,maxHealth,walkSpeed,jumpPower,damage(amount),died,state. The same values as on the player, socharacter.body.walkSpeed = 30andplayer.walkSpeed = 30do the same.character.root- the invisible 2 x 5 x 1 box the character moves and collides as:position(set it to teleport),transform,size,velocity.
character:moveTo(vec3(0, 10, 0)) -- put it there
character:pivotTo(transform.lookAt(vec3(0, 5, 0), vec3(10, 5, 0))) -- there, facing a pointWho touched it?
When a player walks into a part, the part's touched fires with a part of their character. players.fromPart turns that into the player:
script.parent.touched:connect(function(hit)
local player = players.fromPart(hit) -- nil if a crate touched it, not a player
if player then
player:damage(10)
end
end)What the character is doing
body.state is "Idle", "Walking", "Jumping", "Falling", "Climbing", "Sitting" or "Dead":
player.character.body.stateChanged:connect(function(new, old)
if new == "Jumping" then print(player.name .. " jumped") end
end)Dying and respawning
When health reaches 0 the character falls as a ragdoll and stands up again at the spawn (or the last Checkpoint they touched) about 3 seconds later. player.died fires each time.
players.joined:connect(function(player)
player.died:connect(function()
player.stats.Deaths = (player.stats.Deaths or 0) + 1
end)
end)Climbing
Characters climb any part tagged Climbable - a ladder, a vine, a truss. Add the tag in the Inspector's Tags section, or part:addTag("Climbable").
Your own body for players
A world can draw its players as a model of its own - a fox, a robot, a ball with eyes:
- Build a Model facing +Z, standing as it should on the ground.
- Hold the moving pieces to the body with Bones (Animation and rigs).
- Name the Model
StarterCharacterand keep it in ReplicationService. - Optionally add Animations named
Idle,Walk,Jump,FallandClimbinside it.
Every player is then drawn as that model. Their collision box is still KHIM's 2 x 5 x 2, so a model about that size fits best. A server Script can give one player another model: player.characterModel = shared.Wolf (and nil to undo).
Messages in the chat
chat.send("Round 2 starts in 10 seconds!") -- a system line for everyone (a server Script)Kicking
player:kick("You were AFK too long.")To keep someone out for good, ban them from the Workshop's Live page.