Players
Players : Instance
Overview
The Players service manages clients connected to the server as Player objects, offering diverse Player-related functions.
Properties
CharacterAutoLoads
bool
Sets whether characters automatically respawn.
The default is true. If set to false, the player's character won't be created until Player:LoadCharacter() is called, even if they join the game.
This option is particularly useful in games with limited lives or competitive modes that do not allow respawning until the round ends.
Code Samples
local Players = game:GetService("Players")
Players.CharacterAutoLoads = false
UseStrafingAnimations
bool
This property specifies how to handle the direction of an animation played during character movement.
If set to false, only a single movement animation plays regardless of the direction. If set to true, animations for 8-directional movement (i.e., up, down, left, right, and diagonals) play according to the actual movement direction.
This allows natural representation of various movements, such as strafing, backward movement, and diagonal movement, normally required in TPS games.
This property cannot be changed at runtime and should be set in Studio's editing phase.
Code Samples
LocalPlayer
Player
This read-only property references the player object running the game on the current client.
It can only be accessed in client environments, such as LocalScript, and always returns nil in a Script that runs in the server environment.
Code Samples
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
RespawnTime
number
If CharacterAutoLoads is true, this property specifies the respawn wait time for players in seconds.
The default is 5 seconds.
Code Samples
local Players = game:GetService("Players")
Players.RespawnTime = 2
Methods
GetPlayerFromCharacter
Returns the Player object associated with the specified Character, or nil if no player is connected.
Used to retrieve the Player via the character in events like Touched or CharacterAdded, which do not directly return a Player object.
To retrieve a character from a Player object, use the Player.Character property.
Parameters
Model
InCharacter
The character to be converted to Player.
Return
Player
The returned Player.
Code Samples
local Players = game:GetService("Players")
local Part = script.Parent
local function OnTouched(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChild("Humanoid")
if humanoid then
local character = humanoid.Parent
local player = Players:GetPlayerFromCharacter(character)
end
end
Part.Touched:Connect(OnTouched)
GetPlayerByUserId
Returns the Player object associated with the specified UserId, or nil if no such player exists.
Parameters
string
UserId
The UserId of the Player to be converted.The returned Player.
Return
Player
Code Samples
local Players = game:GetService(Players)
local playerId = Players.LocalPlayer.UserId
print(Players:GetPlayerByUserId(playerId))
GetPlayers
Returns a table of all Player objects currently connected to the server.
When used with a for loop, it allows easy iteration over all players in the game.
Parameters
Array
The list of players.
Code Samples
local Players = game:GetService("Players")
local PlayerList = Players:GetPlayers()
for i = 1, #PlayerList do
print(PlayerList[i])
end
Events
PlayerAdded
This event is executed when a player joins the game.
This is mainly used to load saved GlobalDataStore data or apply initial settings when a player joins.
Parameters
Player
player
The connected player.
Code Samples
local Players = game:GetService("Players")
local function OnPlayerAdded(player)
print("[OnPlayerAdded]", player)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
PlayerRemoving
This event is executed just before the player leaves the game.
As it is called before the player is completely removed, it is ideal for saving player data via GlobalDataStore.
Parameters
Player
player
The disconnected player.
Code Samples
local Players = game:GetService("Players")
local function OnPlayerRemoving(player)
print("[OnPlayerRemoving]", player)
end
Players.PlayerRemoving:Connect(OnPlayerRemoving)
Last updated