[Tutorial] How to make an AFK system
#1

OK,so this is good for roleplay servers,but in fact I'm wrong,as it's good for every server.It can be used by admin if only if he wants.I'm going first to start by telling you that commands will be made using ZCMD and sscanf(this is as I know the most used command system).Of course we have two basic command:

-/afk
-/back

What we need first,is at our gamemode top,a global variable.You probably wanna know what is that for?Well that's going to help us to make the commands in that way,that only if you're AFK you can use command /back and only if you are about to be AFK you can use /afk.Now,the variable:

Step 1:

pawn Code:
new PlayerAFK[MAX_PLAYERS];
If you really are a newbie,then as you probably saw on Wiki,****** and other sites,global variables were defined like:

pawn Code:
gGlobalVarName[MAX_PLAYERS];
So you will ask me how can PlayerAFK be a global variable.The point of the fact is that a global variable doesn't really need to have a "g" in the name and you can call it as you wish(e.g:PlayerStatus),but this isn't tutorial of variables usages.Now,step 2(usage of the variable).

Step 2:

There aren't very much things to do with the variable,only the fact that when a player connects you must define it as "0".Why?Because the commands may then consider you already AFK when you try to use them,and you will be able to move and do other things(that only happpens if you define it as "1").

pawn Code:
public OnPlayerConnect(playerid)
{
   PlayerAFK[playerid] = 0;
   return 1;
}
OK,now step 3,the commands.

Step 3:

pawn Code:
COMMAND:afk(playerid,params[])
{
   if(PlayerAFK[playerid] == 0) // checks if the player isn't already AFK(as for AFK the variable is defined as 1)
   {
      new pName[MAX_PLAYER_NAME];
      GetPlayerName(playerid,pName,sizeof(pName)); // this gets your name,we will use it so we can announce when a player is AFK
      new string[120]; // well,we're going to send a message to players,but we must format a string so we can get the player name and show it for the other players
      format(string,sizeof(string),"%s(%d) is now AFK",pName,playerid); // the string message
      TogglePlayerControllable(playerid,0); // this frezees the player,so basically you won't be able to move and nobody will touch you :P
      PlayerAFK[playerid] = 1; //this is the most important,if you remember I said earlier 0 will mean you're in-game and 1 AFK,otherwise the commands will not work(bugged)
      SendClientMessageToAll(COLOR_DEFINED/BY/YOU,string); //sends the string,and the players will know you're AFK so when you come back you will get no messages like "fuck you,why didn't you answer my questions" etc.
    }
    if(PlayerAFK[playerid] == 1) return SendClientMessage(playerid,COLOR_DEFINED/BY/YOU,"You're already AFK) //this checks if you're already AFK and you try to use the command wich is noobish
  return true;
}
pawn Code:
COMMAND:back(playerid,params[])
{
   if(PlayerAFK[playerid] == 0) return SendClientMessage(playerid,COLOR_DEFINED/BY/YOU,"You're not AFK"); //checks if you're not AFK and you try to use the command
   if(PlayerAFK[playerid] == 1) // you're AFK
   {
      new pName[MAX_PLAYER_NAME];
      GetPlayerName(playerid,pName,sizeof(pName)); //we need your name again so we can tell the other players you're back and you can answer their questions :P
      new string[120];
      format(string,sizeof(string),"%s(%d) is now back!",pName,playerid); // the message we gonna send :)
      TogglePlayerControllable(playerid,1); // you can move again,but beware some1 may be behind you and kill you :PP
      SendClientMessageToAll(COLOR_DEFINED/BY/YOU,string); // sends NOW the message that you're back
      PlayerAFK[playerid] = 0; //again the most important thing the fact that you're back is a thing that the variable should also know(xD) or again the commands won't work porperly
    }
  return true;
}
And that's all.I hope you understood it.(It's my first tutorial)
Reply
#2

Lol another one...
Reply
#3

There are TONS of AFK scripts and tutorials... stop that please, really!
Reply
#4

/afk & /back?! i use only /afk for AFK status and /afk for back to game... you can not with conditions
Reply
#5

pawn Code:
if(PlayerAFK[playerid] == 1) return SendClientMessage(playerid,COLOR_DEFINED/BY/YOU,"You're already AFK)
you will need " and ;
Reply
#6

and you can use bool!
Reply
#7

And the system is plain. And it can be abused. And I can spam your server with this etc..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)