Getting online player count

What would be a function to retrieve the player count by number. I’m developing a custom player list and would like to have a number that shows online players instead of you counting you can read the given number.

Thanks for any help

2 Likes

Uhm https://runtime.fivem.net/doc/reference.html You can find a lot on there but what your looking for is : Players.Count, well thats what it is in C# anyway not sure about lua but shouldnt be much different

1 Like

I believe this is for cfx servers

On the server you can use the GET_NUM_PLAYER_INDICIES native. On the client you can count the number of active players in the session using the NETWORK_IS_PLAYER_ACTIVE native in a loop from 0 to 32.

1 Like

Ok thanks all I’m trying todo is have “Online Players: 28” for example. So how exactly would I use these natives?

On the server:

local onlinePlayers = GetNumPlayerIndices()

The client would be something like:

local onlinePlayers = 0
for i = 0, 31 do
    if NetworkIsPlayerActive(i) then
        onlinePlayers = onlinePlayers+1
    end
end

Or as @Scott_UK pointed out:

4 Likes

so the scoreboard script only has a server_script. So could I put

local onlinePlayers = GetNumPlayerIndices()

under this line
local listOn = false

Also how would I reflect this in the html code?
This is my html file

<html>
	<head>
		<script src="nui://game/ui/jquery.js" type="text/javascript"></script>
		<script src="listener.js" type="text/javascript"></script>
		<link rel="stylesheet" href="nui://scoreboard/html/reset.css">
        <link rel="stylesheet" href="nui://scoreboard/html/style.css">
	</head>
	<body>
		<div id="wrap">
			<center><h2 id="tit">IMPROV RP</h2></center>
			<center><h2 id="tit1">Player List</h2></center>
			<table id="ptbl" width="100%">
			</table>
		</div>
	</body>
</html>

I’m confused. These are two contradictory statements. AFAIK, NUI (the HTML stuff) can only be ran from the client. And no, just adding an extra line does not automatically put said line into your scoreboard, that’s something you need to do.

All it is, is that I’m new to coding for FiveM and don’t know how to get that event to display on the scoreboard itself.

1 Like

Surely you could just do this?

local onlinePlayers = GetNumberOfPlayers()

“Gets the number of players in the current session.”

2 Likes

Alternative for client side that I’ve always used is NetworkGetNumConnectedPlayers(). Not sure if there’s any difference in that vs the GetNumberOfPlayers() (and if so, what the difference is).

Hmmm. Still not entirely there on how I would Incorporate this into the scoreboard resource tho.

any updates on this? would much love to be able to put this in the scoreboard

I’m trying to use the native GetNumberOfPlayers() to trigger an event at first connection :

RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(xPlayer)
  	if GetNumberOfPlayers() == 1 then
		TriggerEvent('blabla')
	end
end)

But it always returns 1 for each client loaded in their client console.
Is this a OneSync related limitation or something I did wrong or… ?
Also tried with the native mentioned by Vespura NetworkGetNumConnectedPlayers() but the result remains 1. Don’t understand why.

1 Like

Use #GetActivePlayers().

Thx for your help @nta.
Something like that?

for _, player in ipairs(GetActivePlayers()) do
    local ped = GetPlayerPed(player)
    if player == 1 then
   TriggerEvent('test')
  end
end

I’ve also tried this :

local connected = false

AddEventHandler('playerSpawned', function()
	if not connected then 
		print("first player spawned")
		connected = true
		TriggerEvent('test')
	end
end)

but the print appears in client 2 console, client 3 console…etc and the event is triggered each time a client connects.

No:

if #GetActivePlayers() == 1 then
    -- ...
end

Thx a lot for your reply @nta. Just tried something and it’s working as I wanted, not sure if it’s optimized though :

client side :

AddEventHandler('playerSpawned', function()
	Citizen.Wait(10)
	TriggerServerEvent('lh_park:checkfirstspawn', function(connected)
	end)	
end)

===================================================================================
server side :

local connected = false

RegisterServerEvent('lh_park:checkfirstspawn')
AddEventHandler('lh_park:checkfirstspawn', function()
	if not connected then
		TriggerClientEvent('lh_park:spawnv1', source)
	        connected = true
	end
end)

With this, the event is triggered only at first client connexion.

I also tried #GetActivePlayers() as #GetNumberOfPlayers() and, I don’t know why, it triggers the event when the second client connects.

2 Likes

Solved your question in this topic: Question: Trigger client event with no client connected / Spawn vehicles in server.lua - #12 by 1nsTommy

I tried a few things to get playercount but the only thing that worked is the f*ing dumb for loop

RegisterCommand('pc', function(source, args, rawCommand)
	print("[] GetNumberOfPlayers: " .. GetNumberOfPlayers())
	-- print("[] GetNumPlayerIndices: " .. GetNumPlayerIndices()) -- native is nil?
	print("[] NetworkGetNumConnectedPlayers: " .. NetworkGetNumConnectedPlayers())
	print("[] #GetActivePlayers(): " .. #GetActivePlayers())
	local onlinePlayers = 0
	for i = 0, 1024 do
		if NetworkIsPlayerActive(i) then
			onlinePlayers = onlinePlayers+1
		end
	end
	print("[] NetworkIsPlayerActive: " .. onlinePlayers)
end)

EDIT: Even the for loop is way too weird, it returns 4 players when only i’m online