GET_ACTIVE_PLAYERS: the replacement for player loops

Originally published at: https://cookbook.fivem.net/2019/06/29/get_active_players-the-replacement-for-player-loops/

Just a quick hint: when writing new client-side scripts in Lua/JS (C# already has the Players list doing exactly this), you can loop through players by using the GET_ACTIVE_PLAYERS native. See below for an example of before/after: Before… for i = 0, 255 do if NetworkIsPlayerActive(i) then local ped = GetPlayerPed(i) – do stuff end…

6 Likes

is this still on canary channel?

1 Like

The code after is actually wrong and wont work, you are getting index from the loop and not the player it self, the code needs to be like this

for i,player in ipairs(GetActivePlayers()) do
    local ped = GetPlayerPed(player)
    -- do stuff
end
2 Likes

Thanks, tiny oversight when writing in a blog composer without testing.

Should be fixed now.