AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   problem with the amount of players (https://forums.alliedmods.net/showthread.php?t=241942)

OnePL 06-11-2014 13:25

problem with the amount of players
 
hi,

I used many ways to count the players on the server
Each of them wailed me

I way:
PHP Code:

GetPlayersNum(teamalive) {
    new 
iPlayers;
    for(new 
maxpl0i--) {
        if(!
is_user_connected(i)) continue;

        if(
get_user_team(i) == team && (is_user_alive(i) == alive || alive == 2)) iPlayers++;
    }
    return 
iPlayers;


II way:
PHP Code:

stock GetPlayersNum(teamalive) {
    if(!(
<= team <= 2)) return 0;

    static 
Players[33], num;
    for(new 
1<= maxpli++) {
        if(
get_user_team(i) != team || is_user_alive(i) != alive) continue;

        
Players[++num] = i;
    }
    return 
Players[num];


III way:
PHP Code:

stock GetPlayersNum(teamalive) {
    static 
players[32], numcount;
    
get_players(playersnumalive "ach" "ch");
    for(new 
inumi++)
        if(
get_user_team(players[i]) == teamcount++;

    return 
count;


IV way:
PHP Code:

stock GetPlayersNum(teamalive) {
    for(new 
num1<= maxpli++) {
        if(!
is_user_connected(i) || get_user_team(i) != team) continue;
        if(
alive && !is_user_alive(i)) continue;

        
num++;
    }
    return 
num;


V way:
PHP Code:

stock GetPlayersNum(teamalive) {
    static 
players[32], num;
    
get_players(playersnumalive "ach" "ch"team == "CT" "TERRORIST");
    return 
count;


maxpl = get_maxplayers()

stock is used every second (in task)
every stock working at 90%
10% - sometimes bad counts, for example, when 2 terrorists it shows 7

ironskillz1 06-11-2014 13:47

Re: problem with the amount of players
 
You can try this. https://forums.alliedmods.net/showthread.php?p=1297719

It fixed my problems when i had an jailbreak server

OnePL 06-11-2014 14:11

Re: problem with the amount of players
 
This fix was not implemented in 1.8.2 & 1.8.3?

SpeeDeeR 06-11-2014 14:18

Re: problem with the amount of players
 
get_players(iPlayers, iNum, "ch");
iNum returns the number of players related to the flags passed. There is no need to make a separate function.

ironskillz1 06-11-2014 14:19

Re: problem with the amount of players
 
Quote:

Originally Posted by OnePL (Post 2149978)
This fix was not implemented in 1.8.2 & 1.8.3?

I dont think so. Maybe an person with more knowledge can answer that

OnePL 06-11-2014 15:15

Re: problem with the amount of players
 
Quote:

Originally Posted by SpeeDeeR (Post 2149985)
get_players(iPlayers, iNum, "ch");
iNum returns the number of players related to the flags passed. There is no need to make a separate function.

check V way in first post

SpeeDeeR 06-11-2014 15:48

Re: problem with the amount of players
 
I checked it. You are returning 'count' and you have to return 'num', idk what 'count' is, it isn't even defined.

OnePL 06-11-2014 16:26

Re: problem with the amount of players
 
mistake when copying

OnePL 06-11-2014 20:29

Re: problem with the amount of players
 
get_user_team fix by ConnorMcLeod not solved the problem

Nextra 06-12-2014 05:48

Re: problem with the amount of players
 
PHP Code:

stock GetPlayersNum(teamalive) {
    static 
players[32], numcount;
    
get_players(playersnumalive "ach" "ch");
    for(new 
inumi++)
        if(
get_user_team(players[i]) == teamcount++;

    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(teamalive) {
    if(!(
<= team <= 2)) return 0;

    static 
Players[33], num;
    for(new 
1<= maxpli++) {
        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(teamalive) {
    for(new 
num1<= maxpli++) {
        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(teamalive) {
    static 
players[32], num;
    
get_players(playersnumalive "ach" "ch"team == "CT" "TERRORIST");
    return 
count;


This doesn't compile, count is never defined.

PHP Code:

GetPlayersNum(teamalive) {
    new 
iPlayers;
    for(new 
maxpl0i--) {
        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.


All times are GMT -4. The time now is 09:38.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.