GetNumPlayerIndices() breaks after DropPlayer()

Hello there,
Anyone knows why GetNumPlayerIndices() doesn’t account players that have previously been kicked?

Example:

Player is connected with id 1: GetNumPlayerIndices() returns 1
Player leaves and reconnects (id 2): GetNumPlayerIndices() returns 2
Player reconnects after he has been kicked (id 2): GetNumPlayerIndices() returns 1

I’m observing this on js btw.

Because it returns the amount of player indices, not the highest player index.

Use GetPlayerFromIndex - FiveM Natives @ Cfx.re Docs instead or directly use the GetPlayers wrapper.

Correct, that is what I also thought initially but once I left with my client and reconnected GetNumPlayerIndices() returned 2.

That makes sense since player ids won’t get re-assigned to new players. So the server’s indices probably looked like this:

[1 <-- my previous id before I reconnected (now unused), 2 <-- my current id]

But when my client was dropped (kicked) by the server and afterwards reconnected GetNumPlayerIndices() started to return 1 again. My player id is now 3.

I suppose the servers indices now look like this: [3]
Which would suggest that the DropPlayer() native (maybe other natives as well) clean up the server’s index array from unused ids.

My problem with this is that I can’t get all online players id’s on the server. Just bruteforcing all possible ids with GetPlayerFromIndex() seems like a bad approach to me and the GetPlayers() wrapper doesn’t exist somehow in the JavaScript RE.

Any idea how this could be done properly?

Note: Tested this alone. Latest artifacts and OneSync enabled.

The wrapper is getPlayers() in JS

1 Like

As in your example:

const numIndices = GetNumPlayerIndices(); // in your case, 1
for (let i = 0; i < numIndices; i++) {
    console.log(GetPlayerFromIndex(i)); // 3
}

What are you expecting to be the case here?