06.12.2013, 03:14
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.
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.
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.
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.
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.
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];
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);
}
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;
}
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;
}
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.