FiveM Lua How to create table of all online player id
To create a table of all online player IDs in Lua for FiveM, you can follow these steps:
Declare an empty table variable to store the player IDs:
lua local playerIds = {}
Iterate through all the players on the server using the
GetPlayers
function:lua local players = GetPlayers() for _, playerId in ipairs(players) do -- code to be added end
Inside the loop, check if the player is online and retrieve their ID:
lua local player = GetPlayerFromServerId(playerId) if player and NetworkIsPlayerActive(player) then local playerId = GetPlayerServerId(player) -- code to be added end
Add the player ID to the table:
lua table.insert(playerIds, playerId)
After the loop ends, you will have a table containing all the online player IDs.
Here is the complete code without any introductory or concluding sentences:
local playerIds = {}
local players = GetPlayers()
for _, playerId in ipairs(players) do
local player = GetPlayerFromServerId(playerId)
if player and NetworkIsPlayerActive(player) then
local playerId = GetPlayerServerId(player)
table.insert(playerIds, playerId)
end
end
I hope this helps! Let me know if you have any further questions.