Players

Players : Instance

Overview

The Players class is a service responsible for managing all players in a game. It provides functionality for detecting when players join or leave, accessing player-related data, and handling player-specific interactions. Each connected player is represented by a Player object, which is automatically added to the Players service when they join the game. This service is essential for multiplayer games, as it facilitates communication and interaction between the server and individual players.

Description

The Players class serves as the central hub for managing player-related events and data in OVERDARE UGC. It allows developers to access and manipulate player objects, handle player-specific events, and set up game logic that depends on player actions or attributes.

Key Features

  • Player Management:

    • Automatically adds a Player object to the Players service when a player joins the game.

    • Removes the Player object when the player leaves.

  • Events:

    • PlayerAdded: Fires when a new player joins the game.

    • PlayerRemoving: Fires when a player leaves the game.

    • CharacterAdded: Fires when a player's character spawns or respawns.

    • CharacterRemoving: Fires when a player's character is removed.

  • Accessing Players:

    • Use Players:GetPlayers() to retrieve a list of all current players.

    • Access specific players using their Player.Name or Player.UserId.

  • Player Properties:

    • Each Player object contains properties such as:

      • UserId: The unique identifier for the player's account.

      • Name: The player's username.

      • DisplayName: The player's display name (can differ from their username).

      • AccountAge: The number of days since the account was created.

      • Team: The team to which the player belongs.

      • Character: The player's in-game avatar.

Properties

CharacterAutoLoads

bool

When the character is in a Dead state, decide if you want to respawn again.

Code Samples

local Players = game:GetService("Players")
Players.CharacterAutoLoads = false

RespawnTime

number

RespawnTime controls the amount of time taken for a player's character to respawn.

Code Samples

local Players = game:GetService("Players")
Players.RespawnTime = 2

Methods

GetPlayerFromCharacter

The GetPlayerFromCharacter method retrieves the Player instance associated with a given Model representing a player's character. This is useful for identifying the player controlling a character in the game world.

Parameters

Model InCharacter

Return

Player

Code Samples

local Players = game:GetService("Players") 

local Player = Players:GetPlayerFromCharacter(character)    
print(Player)

GetPlayers

This method returns an array containing all currently connected players in the game. Each player is represented as a Player object, which provides access to various player-specific information and attributes. This is useful for iterating over players or performing operations on all players at once.

Parameters

Return

array

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 triggered whenever a new player joins the game. It allows developers to listen for and respond to player join events, such as initializing player-specific data or setting up the player’s initial state.

Parameters

Code Samples

local Players = game:GetService("Players")

local function OnPlayerAdded(player)
    print("[OnPlayerAdded]", player)
end
Players.PlayerAdded:Connect(OnPlayerAdded)

PlayerRemoving

This event is triggered whenever a player is about to leave the game. It allows developers to listen for and respond to player leave events, such as saving player-specific data or cleaning up resources associated with the player.

Parameters

Code Samples

local Players = game:GetService("Players")

local function OnPlayerRemoving(player)
    print("[OnPlayerRemoving]", player)
end
Players.PlayerRemoving:Connect(OnPlayerRemoving)