Loop through all players on client side? (i always returns 128)

    for _, i in ipairs(GetActivePlayers()) do
        print('Active Players '..GetActivePlayers()) -- ERROR PRINT ON THIS LINE - concatenation issue
        local playerName = GetPlayerName(i)
    
        local label = playerName..' [ID '..i..']'
        print('i = ' ..i)
    end

Could someone explain why the above code always returns 128 when printed?

I’m trying to loop through all players but the print always returns 128.

With this logic, anything I do will only create for ID 128?

Thanks.

  1. GetActivePlayers only returns the players currently present on client side. If you are alone, this will be a list with only your player.
  2. GetActivePlayers returns the client side player index which is usually 128 for your own player.

Thank you for this, how would I loop through all players in the server? GetPlayers doesn’t seem to work.

GetPlayers should work fine on the server. Try to read this part of the docs and also share you current source code with us. :+1:

for OneSync, the client is not aware of all online players, as not all of them are loaded.

What you might want to do is send the GetPlayers() table from server to all clients whenever a player joins or leaves the server.

Would doing this to one client.lua affect all resources?

Thanks for the reply.

I’m creating an admin menu from scratch, using nativeui, most of which is client sided.

    for _, i in ipairs(GetActivePlayers()) do
        print('Active Players'..GetActivePlayers())
        if NetworkIsPlayerActive(i) then 
            local playerName = GetPlayerName(i)
            local id = GetPlayerServerId(i)
    
            local label = playerName..' [ID '..id..']'
            PlayerMenu[i] = MenuPool:AddSubMenu(playerManagementSubMenu, label, "Select Player Options")
            print('i = ', i)
            local playerActions = {"Kick", "Ban", "Warn", "Mute", "Unmute"}
    
            for _, action in ipairs(playerActions) do
                local actionItem = NativeUI.CreateItem(action, "Perform " .. action .. " action on " .. label)
                PlayerMenu[i]:AddItem(actionItem)
    
                actionItem.Activated = function(sender, item, index)
                    ExecutePlayerAction(action, i)
                end
            end
    
            MenuPool:RefreshIndex() -- Refresh the menu pool index after adding items
        end
    end

The logic works, but the looping through all players is an issue, I can’t send it to the server side because NativeUI does not work on the server side; I can never get it to display.