Hi there
I’m using the native ‘GetPlayerName’ in client in Javascript to get all the name of connected players.
Sometimes it output nicely the Steam account name but sometimes (and I can’t figure out why) it output “player 1”, “player 2” (player + my serverID).
Restarting the client wont change anything, I need to reboot the server to get it work ‘properly’ (with a bit of luck).
It may be something that I’m doing wrong, because at the same time the scoreboard ressource correctly display my steam account name everytime but I can’t figure out how the scoreboard actually get the names of all the players.
So I’ve kinda converted the code from es_admin2 to fit my purpose.
Here’s my code :
window.GetPlayerList = () => {
let players = [];
let nPlayers = window.GetNumberOfPlayers();
for (let i = 0; i < nPlayers; i += 1) {
if (window.NetworkIsPlayerActive(i)) {
players.push({
id: window.GetPlayerServerId(i),
name: window.GetPlayerName(i),
})
}
}
return players;
};
Here’s the same thing in es_admin2/client.lua
function getPlayers()
local players = {}
for i = 0,32 do
if NetworkIsPlayerActive(i) then
table.insert(players, {id = GetPlayerServerId(i), name = GetPlayerName(i)})
end
end
return players
end
Later I’ll move this to server-side to add things like ping, it may help, idk.
This won’t work properly all the time, not sure if it’s also the cause for your other issues but at least it won’t add all players to that array.
For example: if player 0, 1, 2, and 3 are online, then that means 4 players are online. so it’ll iterate and find 0, 1, 2, 3. But, if 2 leaves, then you end up with 3 players online, so it’ll find 0, 1, 3. In which case 4 will be left out.
Just loop from 0 to 256 and you’ll be all set.
Not sure if this also gives you the ‘player id’ issue, but at least it’s some problem.
Yeah I’ve read something similar yesterday, don’t know if it was from you but the explanation was the same. I’ll try to loop from 0 to 256 to see if it actually helps with my issue.
Yeah but even with a great and easy explanation sometimes I misunderstand things…
Does this native will output the correct number of connected players everytime? Because I also use it to show to admins the number of connected players. (But I can also use something like players.Lenght to show it, not a big deal)
Thanks you Vespura (and by the way love your work)
GetNumConnectedPlayers() works just fine, but the problem is that it only gives the amount of players currently connected in the same session. So that’s why the looping using that won’t work. But you can use it to show the amount of connected players just fine.
Yeah now I kinda see better.
Anyway it looks like looping from 0 to 256 has solved my issue. I’ve tried it 10 times and GetPlayerName() has gave me 10 times my Steam account name.
Thanks you a lot, solved!