[Tutorial] AFK System - Auto Kick
#1

Introduction

Today while working on my gamemode I thought to create a AFK System which kicks the player out of the server if he hasn't been moved for specific time. For the first few minutes my mind couldn't think of something that checks if player hasn't moved from it's place. But later I figured out I can use a IsPlayerInRangeOfPoint function to compare the position of player after specific time and if it's same, he's definately afk, unless he has been frozen by an admin. Lets go ahead and jump right into the coding.

Lets begin!

1. We'll first create a variable which will hold the value of how long has the player been away from the keyboard. Because there can be more than one player AFK at a time, we'll make the variable an array of all the players in the server. Which will help prevent the future collapse as every player will have it's own AFK variable.

pawn Код:
//On the top of your script
new AFK[MAX_PLAYERS];
new AFKTime[MAX_PLAYERS];
2. When player joins the server we want to start checking the player's X,Y and the Z pos and save it into the variable. In the same function, we want to check if for the specific time the X,Y and the Z pos is the same, kick then out. For that we'll use what is called a timer.

Lets divide it into parts.

Inside the public of OnPlayerConnect, we'll set a timer of 1 second which will repeat every second for the player who joins the server.
pawn Код:
OnPlayerConnect(playerid)
{
   AFKTime[playerid] = SetTimer("AFKCheck",1000,1);
}
The we'll create a public out of the the timer AFKCheck in which we'll have our meat and potatoes. Follow the comments in the script which explains everything inside the public.
pawn Код:
forward AFKCheck();
public AFKCheck()
{
  new Float:Pos[3]; // creating an array variable of 3 values whose datatype is Float.
  for(new i = 0; i < MAX_PLAYERS; i++) // Looping through the players.
  {
     GetPlayerPos(i,Pos[0],Pos[1],Pos[2]); // Gets the player position and saves into their variables.
     if(IsPlayerInRangeOfPoint(i,2,Pos[0],Pos[1],Pos[2])) // If player is around the position (if player is at the same place)
     {
        AFK[i]++; // Increment the AFK variable (for every second if player is at same location, increase the AFK variable by 1)
     }
     if(AFK[i] == 60) // If it has been a minute for the player being at the same place
     {
        AFK[i] = 0; // Reset the afk variable
        format(string,sizeof(string),"%s has been auto-kicked for inactivity.",pName(i));
    SendClientMessageToAll(COLOR_WHITE,string);
        Kick(playerid); //Kick the player.
      }
  }
  return 1;
}
That should give us perfectly working AFK System. Wait a sec... There is something missing.

Lets go through what we've created so far. We know that we have a system that on every second checks player's location, if it's same, then it increases the AFK variable by 1 and if 1 minute has been passed and player is still on same location, server will kick the player.

But what if in the middle of 1 minute player starts moving. It will stop the function right there, which means that the value of AFK is now set to how many minutes he was AFK. But we want to reset it if player starts moving. There is a public out there called OnPlayerUpdate which is called when player is in motion fortunately.

So we can just go and reset out variable inside that function.

pawn Код:
public OnPlayerUpdate(playerid)
{
   AFK[playerid] = 0;
   return 1;
}
Conclusion

This is how easy it is to create a basic AFK System. After making this script I thought to myself, the important tool for scripting is the ideas. The knowledge of scripting can sometimes be useless if you don't have good ideas.
Reply
#2

Very Good Tutorial
Reply
#3

That timer won't work at all...

Every time you connect the player, you start the same timer, so 50 players on‌line = 50 instances of your timer, thus your server will be spammed each second!
Reply
#4

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
That timer won't work at all...

Every time you connect the player, you start the same timer, so 50 players on‌line = 50 instances of your timer, thus your server will be spammed each second!
I realized it. Thanks for pointing it though.
Reply
#5

Nice tutorial,thanks for that.
Reply
#6

I Have a question....

Supposing, a player staying for 60 seconds (not afk) staying in his position, example, he is talking each other with another player, would he get kicked too?

I hope you will get it.
Reply
#7

Yes, you're right it'll consider him as AFK. This is a really basic AFK system so I haven't added any checks to it. If you know how to overcome that, please share.
Reply
#8

Why do you use IsPlayerInRangeOfPoint, why can't you just create a player based timer, check if the coords are still the same?
Reply
#9

Quote:
Originally Posted by Tayab
Посмотреть сообщение
pawn Код:
OnPlayerConnect(playerid)
{
   AFKTime[playerid] = SetTimer("AFKCheck",1000,1);
}
Ah Really?
i think this afk system isn't wrote by you and you just Copy and Paste from a gamemode
Reply
#10

Quote:
Originally Posted by Aliassassin123456
Посмотреть сообщение
Ah Really?
i think this afk system isn't wrote by you and you just Copy and Paste from a gamemode
Nope buddy, i've wrote it all.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)