PHP Code:
stock GetPlayersNum(team, alive) {
static players[32], num, count;
get_players(players, num, alive ? "ach" : "ch");
for(new i; i < num; i++)
if(get_user_team(players[i]) == team) count++;
return count;
}
Using static variables in this way is completely wrong. First and foremost it is pointless premature optimization and secondly it will guarantee that this function will return the wrong result. The count variable is incremented every time this function is called and never reset to zero, so this code will not work.
PHP Code:
stock GetPlayersNum(team, alive) {
if(!(1 <= team <= 2)) return 0;
static Players[33], num;
for(new i = 1; i <= maxpl; i++) {
if(get_user_team(i) != team || is_user_alive(i) != alive) continue;
Players[++num] = i;
}
return Players[num];
}
This is obviously wrong. It will return the highest index that is connected and in the specified team instead of the actual player count. You should return num and remove the
Players array entirely. Also you use a static variable again which will break this code. You can't have tested this for long because this function will quickly fail with index out of bounds errors as you try to access the Players array beyond the valid 32 index.
PHP Code:
stock GetPlayersNum(team, alive) {
for(new num, i = 1; i <= maxpl; i++) {
if(!is_user_connected(i) || get_user_team(i) != team) continue;
if(alive && !is_user_alive(i)) continue;
num++;
}
return num;
}
This is wrong and I would be very surprised if you even managed to compile this. You can't return num here because it has only been defined inside the for loop.
PHP Code:
stock GetPlayersNum(team, alive) {
static players[32], num;
get_players(players, num, alive ? "ach" : "ch", team == 2 ? "CT" : "TERRORIST");
return count;
}
This doesn't compile, count is never defined.
PHP Code:
GetPlayersNum(team, alive) {
new iPlayers;
for(new i = maxpl; i > 0; i--) {
if(!is_user_connected(i)) continue;
if(get_user_team(i) == team && (is_user_alive(i) == alive || alive == 2)) iPlayers++;
}
return iPlayers;
}
This is the only function that looks correct at a first glance. I might have missed something or you haven't actually tested the function in this version, maybe you recreated it from memory and did something different.
I would recommend working off this version and trying it again. I can't believe that this simple function should fail so frequently, so I don't think any bug in get_user_team is responsible. The bugs in get_user_team (that have now been fixed in 1.8.3 anyway) were only exhibited in some specific edge cases, nothing that would fail 1/10 times when run every second.
__________________