Skip to main content

Player

Client Functions

NameDescription
GetServerIDGets serverID from any playerID.
GetIDFromServerIDGets playerID from any serverID.
GetRockstarIDGets rockstarID of local player.

Server Functions

NameDescription
IsConnectedReturns if player is connected.
GetNameGets name of a player.
GetPingGets ping of a player.
KickKicks a specified player.
SetSessionSets session of a player.
GetSessionGets session of a player.
IsSessionActiveReturns whether a player is active in their session.

GetServerID

Get serverID from any playerID.

int serverID = Player.GetServerID(int playerID)

warning

This function only works for players who are in the same session as the local player.

Example:

Events.Subscribe("chatCommand", function(command)
if command == "/myserverid" then
local playerID = Game.GetPlayerId()

Chat.AddMessage("My serverID: " .. Player.GetServerID(playerID))
end
end)

GetIDFromServerID

Get playerID from any serverID.

int playerID = Player.GetIDFromServerID(int serverID)

warning

This function only works for players who are in the same session as the local player.

Example:

Events.Subscribe("chatCommand", function(command)
if command == "/testid" then
local playerID = Game.GetPlayerId()
local serverID = Player.GetServerID(playerID)
local playerID2 = Player.GetIDFromServerID(serverID)

if playerID == playerID2 then
Chat.AddMessage("GetIDFromServerID works!")
end
end
end)

GetRockstarID

Get rockstarID of local player.

int rockstarID = Player.GetRockstarID()

tip

You can use this ID as a unique identifier to assign data or ban players.

Example:

Events.Subscribe("chatCommand", function(command)
if command == "/myrockstarid" then
Chat.AddMessage("My rockstarID: " .. Player.GetRockstarID())
end
end)

IsConnected

Returns if player is connected.

bool connected = Player.IsConnected(int serverID)


GetName

Gets name of a player.

string name = Player.GetName(int serverID)

Example:

Events.Subscribe("testEvent", function()
local source = Events.GetSource()

Console.Log(Player.GetName(source) .. " called testEvent!")
end, true)

GetPing

Gets ping of a player.

int ping = Player.GetPing(int serverID)

Example:

-- in client script:
Events.Subscribe("chatCommand", function(command)
if command == "/myping" then
Events.CallRemote("commandMyPing", {})
end
end)

-- in server script:
Events.Subscribe("commandMyPing", function()
local source = Events.GetSource()

Chat.SendMessage(source, "Your ping is: {0000FF}" .. Player.GetPing(source))
end, true)

Kick

Kicks a specified player.

Player.Kick(int serverID, [optional] string reason)

note

If you want to send a chat message to the player before the kick, you have to create a thread and delay the kick by 1 second, otherwise the message will not reach the player.

Example:

-- in client script:
Events.Subscribe("chatCommand", function(command)
if command == "/kickme" then
Events.CallRemote("commandKickMe", {})
end
end)

-- in server script:
Events.Subscribe("commandKickMe", function()
Player.Kick(Events.GetSource())
end, true)

SetSession

Sets session of a player.

Player.SetSession(int serverID, int sessionID)

note

Valid session ids are from 0-65535. 0 is the default session.

warning

This function only sets which session the player should be in, it does not mean that he is currently in it. Use IsSessionActive to check this.

Example:

-- in client script:
Events.Subscribe("chatCommand", function(command)
if command == "/privatesession" then
Events.CallRemote("commandSession", { true })
elseif command == "/publicsession" then
Events.CallRemote("commandSession", { false })
end
end)

-- in server script:
Events.Subscribe("commandSession", function(private)
local source = Events.GetSource()

if private then
Player.SetSession(source, source)
else
Player.SetSession(source, 0)
end
end, true)

GetSession

Gets session of a player.

int sessionID = Player.GetSession(int serverID)

warning

This function only gets which session the player should be in, it does not mean that he is currently in it. Use IsSessionActive to check this.

Example:

-- in client script:
Events.Subscribe("chatCommand", function(command)
if command == "/mysession" then
Events.CallRemote("commandMySession", {})
end
end)

-- in server script:
Events.Subscribe("commandMySession", function()
local source = Events.GetSource()

Chat.SendMessage(source, "My sessionID: " .. Player.GetSession(source))
end, true)

IsSessionActive

Returns whether a player is active in their session.

bool active = Player.IsSessionActive(int serverID)

note

For example, a player is not active in his session if he is currently switching or lost the connection.

Example:

-- in client script:
Events.Subscribe("chatCommand", function(command)
if command == "/privatesession" then
Events.CallRemote("commandSession", { true })
elseif command == "/publicsession" then
Events.CallRemote("commandSession", { false })
end
end)

-- in server script:
Events.Subscribe("commandSession", function(private)
local source = Events.GetSource()

if private then
Player.SetSession(source, source)
else
Player.SetSession(source, 0)
end

Thread.Create(function()
while not Player.IsSessionActive(source) do
Thread.Pause(50)

if Player.IsSessionActive(source) then
Chat.BroadcastMessage(Player.GetName(source) .. " switched the session!")
end
end
end)
end, true)