# Players

Players : `Instance`

## Overview

The Players service manages clients connected to the server as Player objects, offering diverse Player-related functions

## Properties

### CharacterAutoLoads

`boolean`

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

```lua
local Players = game:GetService("Players")

Players.CharacterAutoLoads = false
```

### 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

```lua
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

```lua
local Players = game:GetService("Players")

Players.RespawnTime = 2
```

### UseStrafingAnimations

`boolean`

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

## Methods

### 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` | The Player object matching the specified UserId, or nil if not found. |
| -------- | --------------------------------------------------------------------- |

#### Code Samples

```lua
local Players = game:GetService(Players)

local playerId = Players.LocalPlayer.UserId
print(Players:GetPlayerByUserId(playerId))
```

### 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

```lua
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)
```

### 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

#### Return

| `Array` | The list of players. |
| ------- | -------------------- |

#### Code Samples

```lua
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

```lua
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

```lua
local Players = game:GetService("Players")

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.overdare.com/development/api-reference/classes/players.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
