GET_ACTIVE_PLAYERS: the replacement for player loops

With changing this a have 2 big problems. Trop items can see and pick all player on the server without being near and second admin menu have problem, I can see only close players…

:slight_smile: :smile:

how to write this in C# ?

List<dynamic> players = API.GetActivePlayers();

foreach (int i in players)
{
  // ...
}

Haven’t tested but should work.

Its returning 0

List<dynamic> players = API.GetActivePlayers();
            foreach (int number in players)
            {
                Main.SendChatMessage("b", number.ToString(),255,0,0);
            }

if ur using oneSync-infinity, this won’t work properly as “infinity” has it’s own client resource control

1 Like

Because you’re alone on a server? Or nobody else in your radius?

1 Like

do you know how to fix it then? get it from server side? if so how?

i,player in ipairs(GetActivePlayers()) do
local ped = GetPlayerPed(player)
– do stuff
end

thanks

Good morning guys. I’ve read your suggestions and opinions many times, but I still have the same problem. I have a base QBcore, which previously only had 64 slots and everything worked fine. The problem was when we upgraded to 128 slots as we had problems with voice chat (fixed) and problem finding players that are further away (in QB-Admin). I checked the solution to change Get players() to GetActivePlayer, however QBCore is a little different and I’m not able to implement the code. Inside the functions.lua (client side) I have this code, which I believe is correct, but I don’t know how to change it without making mistakes. Anyone knowledgeable on this, who can help me?

MSCore.Functions.GetPlayers = function()
    local players = {}
    for _, player in ipairs(GetActivePlayers()) do
        local ped = GetPlayerPed(player)
        if DoesEntityExist(ped) then
            table.insert(players, player)
        end
    end
    return players
end
1 Like

Testing this has left me confused. I am not getting all players connected to the server.

Tested like this: Had a script print #GetActivePlayers() every frame. Had a buddy join my server, teleported next to him.
Script kept on returning 2, fine. Then I teleported some distance away - and the script started returnin 1.

FWIW the same happens if I use the old for i = 0, 255 do loop. Is there no way to get all connected players?

The limitation here seems to be what peds my client has spawned locally. Do I need to get the player list from the server every time?

Hi, on client-side be careful about onesync (check out OneSync 'Infinity': how to use it) :smiley:

1 Like

I was able to get it sorted :slight_smile: Server keeps updating the clients with a player list containing names and coords.

Not sure why, but that native doesn’t appear on the official native reference found here. I’m not saying you’re wrong, as I do believe you are correct, it’s just not documented for some reason.

I got incredibly confused when I could only find a client-side native, but fortunately I had the cookbook article bookmarked and found your post through it.

GetPlayers isn’t actually a native.

https://docs.fivem.net/docs/scripting-reference/runtimes/lua/server-functions/

Or ESX.GetPlayers() On legacy

Are CFX natives at the top of the native reference I linked not actually natives? Or are you saying GetPlayers() is different from those?

Thanks, I wasn’t aware that documentation was a thing when I made my earlier post. I’ll use that more often now. I was previously under the impression that everything was listed in the native reference.