Players and characters
Track player lifecycle, character health, respawn, and teams.
A Player represents a connected player and persists for
the session. Its Character references the current
Model, which contains body parts and a
Humanoid. The character is replaced on respawn.
Track joins and departures
PlayerAdded passes a joining player. Query GetPlayers to initialize players
already connected. Connect first and make initialization safe to repeat.
local Players = game:GetService("Players")
local initialized = {}
local function initialize(player)
if initialized[player] then
return
end
initialized[player] = true
print(player.UserId)
end
Players.PlayerAdded:Connect(initialize)
Players.PlayerRemoving:Connect(function(player)
initialized[player] = nil
end)
for _, player in Players:GetPlayers() do
initialize(player)
endPlayerRemoving fires before removal. Save required session state while the
player is still available.
Access a character
Character may be nil before spawn or between lives. Check it before use.
GetPlayerFromCharacter maps a character model to its player and returns nil
for models with no matching player. A model with a humanoid can also be an NPC.
Health and movement
Humanoid.Health is capped at MaxHealth. Raising MaxHealth does not heal the
character. TakeDamage subtracts a nonnegative amount. Its current implementation does
not check ForceField protection.
WalkSpeed is measured in studs per second. Setting it to 0 stops walking but
does not freeze physics. JumpPower controls jump strength.
Death and respawn
Died fires when health reaches 0. Reconnect character-specific handlers for
each new character. Store state on the Player when it must persist across
respawns.
Players.RespawnTime is the respawn delay in seconds.
Players:RespawnPlayer(player) requests an immediate respawn.
This API does not provide CharacterAdded. Observe new models in workspace
and match them using GetPlayerFromCharacter. Use WaitForChild with a timeout
for character children that may arrive after the model. Also process characters
that already exist when the script starts.
Spawn locations and teams
SpawnLocation defines a spawn position. Enabled
controls availability. Duration is spawn protection time in seconds.
Team instances belong under Teams. Distinct TeamColor
values identify teams. Set a spawn's Neutral to false and match TeamColor
to restrict it to that team. AllowTeamChangeOnTouch changes a player's team on
contact. AutoAssignable permits assignment during team balancing.
Local player
Players.LocalPlayer identifies the player for the current client. It is nil
on the server. Read it from a LocalScript.