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:

  1. Declare an empty table variable to store the player IDs: lua local playerIds = {}

  2. 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

  3. 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

  4. Add the player ID to the table: lua table.insert(playerIds, playerId)

  5. 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.