Issue with loop
#1

Hey guys. I'm having an issue with a loop. I get no errors or anything however it's not doing what I want it to do.

What the situation is.

I want a timer to be able to detect weather a player has gone "afk". AFK meaning standing in the same position for 5 minutes. Once the player has been standing in the same position for 5 minutes SendClientMessageToAll stating that Name has gone AFK.

PHP код:
public AFKCheck()
{
    new 
Float:xFloat:yFloat:zstring[128];
    for (new 
iGetPlayerPoolSize(); <= ji++)
    {
        if(!
IsPlayerConnected(i)) continue;
        if(
IsAFK[i] == 1) return 0;
        
GetPlayerPos(i,x,y,z); 
        if(
IsPlayerInRangeOfPoint(i,2,x,y,z)) 
        {
            
AFK[i]++;
        }
        if(
AFK[i] == 300
        {
            
format(string,sizeof(string),"*%s has gone AFK",GetName(i));//Formats the /do cmd output
            
SendClientMessageToAll(COLOR_WHITE,string);
            
IsAFK[i] = 1;
        }
    }
    return 
1;

This is the code I have however the problem is. Whenever no one touches their keyboard and goes "AFK" how ever many people are connected go "AFK" for example if there are three people connected in the server; Named Bob, Test and John. If John(ID:3) goes AFK you get John has gone AFK, Bob has gone AFK, Test has gone AFK. If Bob and Test are active it immediately says Bob is back, Test is back. No matter how many people are in the server, it will always display all the names. So loop isn't working properly? Or am I just doing it wrong? Could it be the timer?

PHP код:
AFKTime[playerid] = SetTimer("AFKCheck",1000,1); 
Reply
#2

PHP код:
AFKTime[playerid] = SetTimer("AFKCheck",1000,1); 
this only has to be under OnGameModeInit / OnFilterScriptinit Once as long as you looping through all players by it
PHP код:
public OnGamemodeInit()
{
    
SetTimer("AFKCheck",1000,1); 

PHP код:
if(IsAFK[i] == 1) return 0
you had to use continue here for ex if id 0 is afk it won't check if others players will be afk or no....

PHP код:
public AFKCheck() 

    new 
Float:xFloat:yFloat:zstring[128]; 
    for (new 
iGetPlayerPoolSize(); <= ji++) 
    { 
        if(!
IsPlayerConnected(i)) continue; 
        if(
IsAFK[i] == 1) continue; 
        
GetPlayerPos(i,x,y,z);  
        if(
IsPlayerInRangeOfPoint(i,2,x,y,z))  
        { 
            
AFK[i]++; 
        } 
        if(
AFK[i] == 300)  
        { 
            
format(string,sizeof(string),"*%s has gone AFK",GetName(i));//Formats the /do cmd output 
            
SendClientMessageToAll(COLOR_WHITE,string); 
            
IsAFK[i] = 1
        } 
    } 
    return 
1

Reply
#3

PHP код:
GetPlayerPos(i,x,y,z);  
        if(
IsPlayerInRangeOfPoint(i,2,x,y,z))  
        { 
            
AFK[i]++; 
        } 
This will almost always be considered 'true'... so even if players aren't AFK and are constantly moving, the server will still consider them 'AFK'.
Reply
#4

Quote:
Originally Posted by Threshold
Посмотреть сообщение
PHP код:
GetPlayerPos(i,x,y,z);  
        if(
IsPlayerInRangeOfPoint(i,2,x,y,z))  
        { 
            
AFK[i]++; 
        } 
This will almost always be considered 'true'... so even if players aren't AFK and are constantly moving, the server will still consider them 'AFK'.
How would I set it so that if player is there that they aren't set afk.
Reply
#5

Something like this

pawn Код:
public AFKCheck()
{
    static Float:LastPos[MAX_PLAYERS][3];
    new Float:x, Float:y, Float:z, string[128];
    for(new j = GetPlayerPoolSize(); i > -1; i--)
    {
        if(!IsPlayerConnected(i) || IsAFK[i]) continue; // add here some !IsPlayerSpawned(i)

        GetPlayerPos(i,x,y,z);
        if(IsPlayerInRangeOfPoint(i,2.0,LastPos[i][0],LastPos[i][1],LastPos[i][2]))
        {
            if(IsAFK[i] == 0 && ++AFK[i] >= 300)
            {
                IsAFK[i] = 1;
                format(string,sizeof(string),"*%s has gone AFK",GetName(i));//Formats the /do cmd output
                SendClientMessageToAll(COLOR_WHITE,string);
            }
        }
        else
        {
            if(IsAFK[i] == 1 && AFK[i] >= 300)
            {
                IsAFK[i] = 0;
                AFK[i] = 0;
                // player is back
            }
            LastPos[i][0] = x;
            LastPos[i][1] = y;
            LastPos[i][2] = z;
        }
    }
    return 1;
}
Reply
#6

Small issue with your 'i's and 'j's there Iefff. (See what I did there? )
Reply
#7

warning 204: symbol is assigned a value that is never used: "j"
PHP код:
public AFKCheck()
{
    static 
Float:LastPos[MAX_PLAYERS][3];
    new 
Float:xFloat:yFloat:zstring[128];
    for(new 
iGetPlayerPoolSize(); > -1i--)
    {
        if(!
IsPlayerConnected(i) || IsAFK[i]) continue; // add here some !IsPlayerSpawned(i)
        
GetPlayerPos(i,x,y,z);
        if(
IsPlayerInRangeOfPoint(i,2.0,LastPos[i][0],LastPos[i][1],LastPos[i][2]))
        {
            if(
IsAFK[i] == && ++AFK[i] >= 300)
            {
                
IsAFK[i] = 1;
                
format(string,sizeof(string),"*%s has gone AFK",GetName(i));//Formats the /do cmd output
                
SendClientMessageToAll(COLOR_WHITE,string);
            }
        }
        else
        {
            if(
IsAFK[i] == && AFK[i] >= 300)
            {
                
IsAFK[i] = 0;
                
AFK[i] = 0;
                
// player is back
            
}
            
LastPos[i][0] = x;
            
LastPos[i][1] = y;
            
LastPos[i][2] = z;
        }
    }
    return 
1;

Reply
#8

Change the 'j' to an i...

PHP код:
for(new GetPlayerPoolSize(); > -1i--) 
Reply
#9

and remove this part
pawn Код:
|| IsAFK[i]
btw stupid idea with GetPlayerPoolSize() it returns 0 when there is no players so loop is always scanning ID 0 Pool should returns -1 mr Kalcor
Reply
#10

The thing now. Is that even if a player isn't AFK and are moving around they are being set to AFK.

PHP код:
public AFKCheck()
{
    static 
Float:LastPos[MAX_PLAYERS][3];
    new 
Float:xFloat:yFloat:zstring[128];
    for(new 
MAX_PLAYERS> -1i--)
    {
        if(!
IsPlayerConnected(i) || !IsPlayerSpawned(i)) continue; // add here some !IsPlayerSpawned(i)
        
GetPlayerPos(i,x,y,z);
        if(
IsPlayerInRangeOfPoint(i,2.0,LastPos[i][0],LastPos[i][1],LastPos[i][2]))
        {
            if(
IsAFK[i] == && ++AFK[i] >= 300)
            {
                
IsAFK[i] = 1;
                
format(string,sizeof(string),"*%s has gone AFK",GetName(i));//Formats the /do cmd output
                
SendClientMessageToAll(COLOR_WHITE,string);
            }
        }
        else
        {
            if(
IsAFK[i] == && AFK[i] >= 300)
            {
                
IsAFK[i] = 0;
                
AFK[i] = 0;
                
// player is back
            
}
            
LastPos[i][0] = x;
            
LastPos[i][1] = y;
            
LastPos[i][2] = z;
        }
    }
    return 
1;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)