Syntax
Players.PlayerAdded:Connect(onPlayer)
for _, player in Players:GetPlayers() do
onPlayer(player)
endExamples
Handle new and existing players
Script in ServerScriptService. The seen guard avoids duplicate setup. The character handler is non-yielding here; larger systems should keep connection cleanup and character ownership explicit.
local Players = game:GetService("Players")
local seen = {}
local function onPlayer(player)
if seen[player] then return end
local function onCharacter(character)
print(player.Name, "spawned", character.Name)
end
seen[player] = player.CharacterAdded:Connect(onCharacter)
if player.Character then onCharacter(player.Character) end
end
Players.PlayerAdded:Connect(onPlayer)
Players.PlayerRemoving:Connect(function(player)
if seen[player] then seen[player]:Disconnect() end
seen[player] = nil
end)
for _, player in Players:GetPlayers() do onPlayer(player) endWait for your current character
LocalScript in StarterPlayerScripts. This obtains one current character for a one-time operation. To keep a feature working after respawn, move setup into CharacterAdded instead.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid", 10)
if humanoid and humanoid:IsA("Humanoid") then
print("Current health:", humanoid.Health)
endBest practices
- Use UserId for persistent identity; display names and usernames can change.
- Do not retain an old character reference across respawns.
- PlayerRemoving is a cleanup opportunity, not a guarantee that a network save will succeed.
At a glance
- Purpose
- Typed scripting and Roblox development
- File extension
- .luau
- Runs in
- Luau host; Roblox engine examples require Roblox Studio
- Usually used with
- Roblox APIs and Studio
Specifications & further reading
Related Luau documentation
Humanoid, HumanoidRootPart and attributes
Humanoid controls character behaviour such as health, while HumanoidRootPart gives a useful spatial reference. Attributes attach small named values to Instances. They are convenient for configuration and display, but client-side attributes are not proof that a player earned a reward.Load and validate player data
A successful read returning nil means no stored value exists. A failed read means the value is unknown. Those cases must never share a fallback that later saves defaults over real data. Validate stored fields before marking a session ready.Events, connections and cleanup
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.Script, LocalScript and ModuleScript placement
The same code can behave differently depending on where it runs. These lessons use server Scripts in ServerScriptService and LocalScripts in StarterPlayerScripts or StarterGui. A ModuleScript runs when required. Script RunContext also affects execution, so inspect it when a script appears silent.
Humanoid controls character behaviour such as health, while HumanoidRootPart gives a useful spatial reference. Attributes attach small named values to Instances. They are convenient for configuration and display, but client-side attributes are not proof that a player earned a reward.Load and validate player data
A successful read returning nil means no stored value exists. A failed read means the value is unknown. Those cases must never share a fallback that later saves defaults over real data. Validate stored fields before marking a session ready.Events, connections and cleanup
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.Script, LocalScript and ModuleScript placement
The same code can behave differently depending on where it runs. These lessons use server Scripts in ServerScriptService and LocalScripts in StarterPlayerScripts or StarterGui. A ModuleScript runs when required. Script RunContext also affects execution, so inspect it when a script appears silent.