Getting a list of all players

I am trying to get a list of all players currently on the server. I have had a look through the reference manual but I couldn’t see anything that did what I needed it to do. Basically, I am trying to do a similar thing to what the Lambda menu does in its ‘Online Players’ section.

This is what I have:

function GetPlayers()
    local players = {}

    for i = 0, 31 do
        if NetworkIsPlayerActive(i) then
            table.insert(players, i)
        end
    end

    return players
end

This is where I am calling the function:

local players = GetPlayers()
for i = 0, 31 do
    --If any player was selected:
    if WarMenu.Button(players[i]) then
        --Do Something here:
    end
end

When I join the server and look at this section of the menu, it generates 32 empty slots with no player names, also I am the only person joining meaning that it should only create 1 slot.

Any help would greatly appreciated.

Try this

local players = GetPlayers()
for _, i in ipairs(players) do
    --If any player was selected:
    if WarMenu.Button(players[i]) then
        --Do Something here:
    end
end

It should work…right?

Thanks for the response. That worked in the way that it only detected the one player which was me. The question now is, how to get it to display the player’s name?

Also, on another note, could you explain the difference in the for loop that I had and the one you provided?

Give an hour and i will be on my pc

Awesome thanks. :smile:

Try this

local players = GetPlayers()
for _, i in ipairs(players) do
local pnames = GetPlayerName(i)
    --If any player was selected:
    if WarMenu.Button(pnames) then
        --Do Something here:
    end
end

3 Likes

Yes that worked! Thanks. Where did you find these functions? Is there a reference for them somewhere?

You have all native functions here: https://runtime.fivem.net/doc/natives and the doc here: https://docs.fivem.net

Add me on discord if you need anything else. Im not looking at the forums that often. Big Yoda#7911

Awesome thanks for all your help guys.