Skip to content

Players and characters ​

Players joining and leaving ​

lua
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)
end

In a ClientScript, players.me is the player whose device it runs on.

A player ​

MemberWhat it is
name, displayNameTheir username, and the name shown
userIdTheir account number; -1 for a guest
health, maxHealthHealth (100 to start). Set health = 0 to kill
walkSpeedUnits a second (20 to start, 0-200)
jumpPowerHow hard they jump (50 to start, 0-200)
positionWhere they are; set it from a Script to teleport
team, neutralTheir team
statsTheir stats in the player list (below)
backpackTheir tools
characterTheir body in the world (below)
guiTheir copy of the UI (ClientScripts)
characterModelA Model to draw them as instead of KHIM's body (below)
diedEvent: 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
lua
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):

lua
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, so character.body.walkSpeed = 30 and player.walkSpeed = 30 do 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.
lua
character:moveTo(vec3(0, 10, 0))                       -- put it there
character:pivotTo(transform.lookAt(vec3(0, 5, 0), vec3(10, 5, 0)))  -- there, facing a point

Who 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:

lua
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":

lua
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.

lua
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:

  1. Build a Model facing +Z, standing as it should on the ground.
  2. Hold the moving pieces to the body with Bones (Animation and rigs).
  3. Name the Model StarterCharacter and keep it in ReplicationService.
  4. Optionally add Animations named Idle, Walk, Jump, Fall and Climb inside 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 ​

lua
chat.send("Round 2 starts in 10 seconds!")   -- a system line for everyone (a server Script)

Kicking ​

lua
player:kick("You were AFK too long.")

To keep someone out for good, ban them from the Workshop's Live page.