Skip to content

Players

System · Players · server and client scripts

Players looks up connected players and their characters, checks admin status, and disconnects players. Subscribe to player and character lifecycle events through Component.User.Added:Connect(...), Component.User.Removed:Connect(...), and Component.Character.Added:Connect(...).

All

Function
All() -> {Entity}

All returns an array of every player currently in the place.

Example

-- Server script.
for _, player in Players.All() do
    print(player.Name)
end

Local

PropertyClient onlyRead only
Local: Entity?

Local is the local player on the client, or nil before the local player reaches the client. Reading Players.Local on the server raises an error.

Errors (1)
Error When
Local can only be written from a client script Written from a server script.

Of

Function
Of(e: Entity) -> Entity?

Of returns the player that owns entity (a player or character entity), or nil if none owns it.

Example

-- Server script.
Component.Character.Added:Connect(function(character: Entity)
    local player = Players.Of(character)
    if player then
        print(player.Name)
    end
end)

IsAdmin

Function
IsAdmin(player: Entity) -> boolean

IsAdmin returns whether player has place administrator permissions.

Example

-- Server script.
Component.User.Added:Connect(function(player: Entity)
    print(player.Name, Players.IsAdmin(player))
end)

Get

Function
Get(userId: number) -> Entity?

Get returns the player with the given user ID, or nil.

Example

-- Server script.
Component.User.Added:Connect(function(player: Entity)
    local user = player.User
    if user then
        print(Players.Get(user.Id) == player)
    end
end)

Kick

FunctionServer only
Kick(player: Entity, reason: string?) -> boolean

Kick disconnects a player from the server and shows them the reason.

Example

-- Server script.
Chat.MessageSent:Connect(function(player, message)
    if message == "!leave" then
        Players.Kick(player, "Goodbye")
    end
end)
Errors (1)
Error When
Kick can only be called from the server Called from a client script.

Friends

FriendStatus

FunctionClient only
FriendStatus(player: Entity) -> FriendStatus

FriendStatus returns the relation of the local player to another player, one of the FriendStatus strings. Before the first relation reply arrives, it returns "NotFriend". Only client scripts may call it.

Example

-- Client script.
for player in World.Query(Component.User) do
    if player ~= Players.Local then
        print(player.Name, Players.FriendStatus(player))
    end
end
Errors (1)
Error When
FriendStatus can only be called from a client script Called from a server script.

FriendStatusChanged

EventClient onlyRead only
FriendStatusChanged: Signal<(player: Entity, status: FriendStatus) -> ()>

FriendStatusChanged fires with a player and the new FriendStatus when the local player's relation to that player changes.

Example

-- Client script.
Players.FriendStatusChanged:Connect(function(player, status)
    local user = player.User
    if status == "FriendRequestReceived" and user then
        print(user.DisplayName .. " sent you a friend request")
    end
end)
Errors (1)
Error When
FriendStatusChanged can only be written from a client script Written from a server script.

AreFriends

FunctionServer only
AreFriends(a: Entity, b: Entity) -> boolean

AreFriends returns whether two players are friends and neither has blocked the other. It returns false when both arguments refer to the same player. Only server scripts may call it.

Example

-- Server script.
Component.User.Added:Connect(function(joined)
    for player in World.Query(Component.User) do
        if player ~= joined and Players.AreFriends(joined, player) then
            print(joined.Name .. " and " .. player.Name .. " are friends")
        end
    end
end)
Errors (1)
Error When
AreFriends can only be called from the server Called from a client script.

FriendshipChanged

EventServer onlyRead only
FriendshipChanged: Signal<(a: Entity, b: Entity, friends: boolean) -> ()>

FriendshipChanged fires when a friendship between two present players begins or ends, passing both players and whether they are now friends. Only server scripts may use it.

Example

-- Server script.
Players.FriendshipChanged:Connect(function(a, b, friends)
    print(a.Name, b.Name, friends)
end)
Errors (1)
Error When
FriendshipChanged can only be written from the server Written from a client script.

Badge

Function
Badge(player: Entity) -> string?

Badge returns the platform badge of a player as a string, or nil when the player has none. The platform decides which strings exist; the engine passes them through unchanged. Both server and client scripts may call it.

Example

-- Server or client script.
for player in World.Query(Component.User) do
    print(player.Name, Players.Badge(player))
end

BadgeIcon

Function
BadgeIcon(badge: string) -> string?

BadgeIcon returns the decimal texture asset ID for a platform badge name ("cadet", "specialist", or "commander"), or nil for an unknown name.

Example

-- Server or client script.
for player in World.Query(Component.User) do
    local badge = Players.Badge(player)
    local icon = if badge then Players.BadgeIcon(badge) else nil
    print(player.Name, icon)
end

BadgeChanged

EventRead only
BadgeChanged: Signal<(player: Entity, badge: string?) -> ()>

BadgeChanged fires with a player and their new Badge string, or nil when the badge is cleared, for each present player whose badge changed.

Example

-- Server or client script.
Players.BadgeChanged:Connect(function(player, badge)
    print(player.Name, badge)
end)