Player functions

Access Player properties and methods After fetching a player instance, you can access its properties and methods using the dot (.) notation. For example, to get the player's username, use:

local username = newPlayer:GetUsername()

Similarly, you can call other methods available in the Player class, such as:

local id = newPlayer:GetId()
local character = newPlayer:GetCharacter()
local level = newPlayer:GetLevel()

To set properties, use the same dot notation:

newPlayer:SetUsername("NewUsername")
newPlayer:SetLevel(5)

Interact with the PlayerManager The PlayerManager allows you to interact with multiple players. To get the global instance of the PlayerManager, use:

local playerManager = PlayerManager:get()

Now you can call methods from the PlayerManager class:

local playerCount = playerManager:Count()
local player = playerManager:GetByConnectionId(connId)
local allPlayers = playerManager:GetAllPlayers()

Iterate over all players The GetAllPlayers function returns a table (Lua's version of an array) containing all players. To iterate over all players and perform actions, use a loop:

local allPlayers = playerManager:GetAllPlayers()
for _, player in ipairs(allPlayers) do
    local username = player:GetUsername()
    print("Player username: " .. username)
end

Last updated