[Tutorial] AFK System
#1

AFK System
A Tutorial on how to create an AFK system
Why did you make this ?
I know that there is a lot of these tutorials on how to make an AFK system, but since i am new to the forum, i wanted to make something like this so i made this, Please do not criticize my work
About the Script
This script is a normal AFK / BRB Script that uses zcmd and you can check how many players are AFK
What do we need
1. ZCMD
Let's get started
1. First of all open a new Script / Existing script (it doesn't matter)
2. Make an Enum above your code, under your defines, to store your AFK status
pawn Код:
enum pInfo
{
    AFK
}
new PlayerInfo[MAX_PLAYERS][pInfo];
if you have an enum you can integrate AFK into it however it is optional
3. Create the AFK Command using ZCMD
pawn Код:
CMD:afk(playerid, params[])
{
    new string[128]; // Declaring the string as 128 bit text
    if(PlayerInfo[playerid][AFK] == 1) // if the enum we created earlier = 1 (which means the player is already AFK) then
    {
        SendClientMessage(playerid, COLOR_RED, "You Are Already AFK"); // Send him a message informing him he can't use /afk again because he is already AFK
    }
    else if(PlayerInfo[playerid][AFK] == 0) // If the enum = 0 (which means the player is not AFK) then
    {
        new pName[MAX_PLAYER_NAME]; // Declaring pName As MAX_PLAYER_NAME
        GetPlayerName(playerid, pName, sizeof(pName)); // Getting Player's name and filling it into pName
        format(string, sizeof(string), "%s Is Now AFK", pName); // formatting our string that we defined earlier
        SendClientMessageToAll(COLOR_RED, string); // Sending a Client message to all informing them that pName used /afk and he is AFK now
        TogglePlayerControllable(playerid, 0); // Freeze the player so he can't (walk, drive, run, etc..)
        PlayerInfo[playerid][AFK] = 1; // Change the enum we created to 1 (which means he is already AFK)
    }
    return 1; // return 1 (means that the operation completed successfully
}
NOTE: new pName[MAX_PLAYER_NAME] is the same as new pName[24];
4. Now create the BACK command using ZCMD
pawn Код:
CMD:back(playerid, params[])
{
    new string[128];// Declaring the string as 128 bit text
    if(PlayerInfo[playerid][AFK] == 0) // if the enum we created earlier = 0 (which means the player is not AFK) then
    {
        SendClientMessage(playerid, COLOR_RED, "You Are Not AFK"); // Send him a message telling him that his is not AFK so he can't use /back
    }
    else if(PlayerInfo[playerid][AFK] == 1) // if the enum = 1 (which means he is already AFK when he wrote this command) then
    {
        new pName[MAX_PLAYER_NAME]; // declare pName as MAX_PLAYER_NAME also as we said you can use new pName[24];
        GetPlayerName(playerid, pName, sizeof(pName)); // Getting player's name and filling it into pName
        format(string, sizeof(string), "%s Is Now Back"); // formatting our string that we define earlier
        SendClientMessageToAll(COLOR_RED, string); // Sending all of the players that this player is back and not AFK
        TogglePlayerControllable(playerid, 1); // Unfreeze the player from his frozen state so he can (walk, talk, drive, etc..)
        PlayerInfo[playerid][AFK] = 0; // Set the enum to 0 means the player is not AFK
    }
    return 1; // return 1 (means that the operation completed successfully
}
5. Now under your OnPlayerConnect Add this
pawn Код:
PlayerInfo[playerid][AFK] = 0;
So when the player joins it tells the server he is not AFK
6. Under your OnPlayerText add this
pawn Код:
public OnPlayerText(playerid, text[])
{
        if (PlayerInfo[playerid][AFK] == 1)
        {
                SendClientMessage(playerid, COLOR_RED, "You Are In AFK State Use /back To Speak Again");
                return 0;
        }

        else if (PlayerInfo[playerid][AFK] == 0)
        {
                return 1;
        }
        return 1;
}
That code ^ won't let you speak (chat) if you are in the AFK state (means if you enum that we created = 1) however if the enum = 0 then you can normally chat
7. Now Let's create the /whoisafk Command
pawn Код:
CMD:whoisafk(playerid, params[])
{
    new count = 0; // declaring count as 0 (meaning no one is AFK first)
    new pName[MAX_PLAYER_NAME]; // declaring pName as the player name
    new string[128]; // declaring string as 128 bit text
    GetPlayerName(playerid, pName, sizeof(pName)); // getting the player's name and filling it into pName
    for(new i = 0;i<MAX_PLAYERS;i++)
    {
        if(IsPlayerConnected(i)) // if the player is connected
        {
            if(PlayerInfo[i][AFK] == 1) // if the player's enum = 1 then
            {
                format(string, 128, "AFK>%s",pName,i); // formatting our SendClientMessage
                SendClientMessage(playerid, COLOR_RED, string); // using our SendClientMessage
                count++; // Increase the players
            }
        }
    }
    if(count == 0) // if there is nobody AFK then
    {
        SendClientMessage(playerid, COLOR_RED, "No One Is AFK"); // sending a client message to the player telling him that no one is afk
    }
    return 1;
}
Additionally
You can make a command for the afk commands i know it is easy to make it is just HaZaRaS gave me a suggestion and i was bored so i added it
pawn Код:
CMD:afkhelp(playerid, params[])
{
    SendClientMessage(playerid, COLOR_RED, "===========================");
    SendClientMessage(playerid, COLOR_RED, "> /whoisafk, >afk, >back");
    SendClientMessage(playerid, COLOR_RED, "===========================");
    return 1;
}
Conclusion
That is it tutorial done i hope you understood something from it, and as i said above please do not criticize my work if it doesn't like you, if you have any suggestions / found bugs please tell me
Reply
#2

one problem
pawn Код:
CMD:afk(playerid, params[])
{
    new string[128]; // Declaring the string as 128 bit text
    if(PlayerInfo[playerid][AFK] == 1) // if the enum we created earlier = 1 (which means the player is already AFK) then
    {
        SendClientMessage(playerid, COLOR_RED, "You Are Already AFK"); // Send him a message informing him he can't use /afk again because he is already AFK
    }
    else if(PlayerInfo[playerid][AFK] == 0) // If the enum = 0 (which means the player is not AFK) then
    {
        new pName[MAX_PLAYER_NAME]; // Declaring pName As MAX_PLAYER_NAME
        GetPlayerName(playerid, pName, sizeof(pName)); // Getting Player's name and filling it into pName
        format(string, sizeof(string), "%s Is Now AFK", pName); // formatting our string that we defined earlier
        SendClientMessageToAll(COLOR_RED, string); // Sending a Client message to all informing them that pName used /afk and he is AFK now
        TogglePlayerControllable(playerid, 0); // Freeze the player so he can't (walk, drive, run, etc..)
        PlayerInfo[playerid][AFK] = 1; // Change the enum we created to 1 (which means he is already AFK)
    }
    return 1; // return 1 (means that the operation completed successfully
}
}
you created two brackets at finish
Reply
#3

Quote:
Originally Posted by Skaizo
Посмотреть сообщение
one problem
pawn Код:
CMD:afk(playerid, params[])
{
    new string[128]; // Declaring the string as 128 bit text
    if(PlayerInfo[playerid][AFK] == 1) // if the enum we created earlier = 1 (which means the player is already AFK) then
    {
        SendClientMessage(playerid, COLOR_RED, "You Are Already AFK"); // Send him a message informing him he can't use /afk again because he is already AFK
    }
    else if(PlayerInfo[playerid][AFK] == 0) // If the enum = 0 (which means the player is not AFK) then
    {
        new pName[MAX_PLAYER_NAME]; // Declaring pName As MAX_PLAYER_NAME
        GetPlayerName(playerid, pName, sizeof(pName)); // Getting Player's name and filling it into pName
        format(string, sizeof(string), "%s Is Now AFK", pName); // formatting our string that we defined earlier
        SendClientMessageToAll(COLOR_RED, string); // Sending a Client message to all informing them that pName used /afk and he is AFK now
        TogglePlayerControllable(playerid, 0); // Freeze the player so he can't (walk, drive, run, etc..)
        PlayerInfo[playerid][AFK] = 1; // Change the enum we created to 1 (which means he is already AFK)
    }
    return 1; // return 1 (means that the operation completed successfully
}
}
you created two brackets at finish
Agreed and there are thousends of this script and filterscript .Prett easy but again good try. it whould be more useful if you chould do
pawn Код:
SetplayerName(playerid, "[AFK]%s"));
Reply
#4

Come on guys give him a break it is his first share
Reply
#5

Quote:
Originally Posted by Skaizo
Посмотреть сообщение
one problem
pawn Код:
CMD:afk(playerid, params[])
{
    new string[128]; // Declaring the string as 128 bit text
    if(PlayerInfo[playerid][AFK] == 1) // if the enum we created earlier = 1 (which means the player is already AFK) then
    {
        SendClientMessage(playerid, COLOR_RED, "You Are Already AFK"); // Send him a message informing him he can't use /afk again because he is already AFK
    }
    else if(PlayerInfo[playerid][AFK] == 0) // If the enum = 0 (which means the player is not AFK) then
    {
        new pName[MAX_PLAYER_NAME]; // Declaring pName As MAX_PLAYER_NAME
        GetPlayerName(playerid, pName, sizeof(pName)); // Getting Player's name and filling it into pName
        format(string, sizeof(string), "%s Is Now AFK", pName); // formatting our string that we defined earlier
        SendClientMessageToAll(COLOR_RED, string); // Sending a Client message to all informing them that pName used /afk and he is AFK now
        TogglePlayerControllable(playerid, 0); // Freeze the player so he can't (walk, drive, run, etc..)
        PlayerInfo[playerid][AFK] = 1; // Change the enum we created to 1 (which means he is already AFK)
    }
    return 1; // return 1 (means that the operation completed successfully
}
}
you created two brackets at finish
Edited
Reply
#6

Quote:
Originally Posted by Mr.Fames
Посмотреть сообщение
Come on guys give him a break it is his first share
Thank you
Reply
#7

If you're checking their AFK status when they type /AFK....Why not just disable it when they type it again? Also, if you're checking it when they type...Why not make them non afk once they're back? One more thing...

pawn Код:
else if (PlayerInfo[playerid][AFK] == 0)
        {
                return 1;
        }
We don't need this check at all. We aren't going to do anything anyway. Maybe not just one more thing...We can use a global variable that increments and decrements when they go afk/come back from afk instead of using a loop.

Hmm..I'm not sure if it's just me...Isn't PlayerInfo[MAX_PLAYERS][pInfo]; kind of redundant? I mean, I know it's going to be for players when doing Info[playerid][Something]...I see it so much and just want to rage..At least you didn't do PlayerInfo[playerid][pAFK] instead...That one gets me real mad... It's a good attempt but I think you could improve it a lot. You could also teach people about booleans instead of using regular 0 1.
Reply
#8

Quote:
Originally Posted by [ABK]Antonio
Посмотреть сообщение
If you're checking their AFK status when they type /AFK....Why not just disable it when they type it again? Also, if you're checking it when they type...Why not make them non afk once they're back? One more thing...

pawn Код:
else if (PlayerInfo[playerid][AFK] == 0)
        {
                return 1;
        }
We don't need this check at all. We aren't going to do anything anyway. Maybe not just one more thing...We can use a global variable that increments and decrements when they go afk/come back from afk instead of using a loop.

Hmm..I'm not sure if it's just me...Isn't PlayerInfo[MAX_PLAYERS][pInfo]; kind of redundant? I mean, I know it's going to be for players when doing Info[playerid][Something]...I see it so much and just want to rage..At least you didn't do PlayerInfo[playerid][pAFK] instead...That one gets me real mad... It's a good attempt but I think you could improve it a lot. You could also teach people about booleans instead of using regular 0 1.
I just learned pawn and i wanted to make my first share here on forums so i decided to make a tutorial
Reply
#9

looks good
Reply
#10

Quote:
Originally Posted by Saad_
Посмотреть сообщение
I just learned pawn and i wanted to make my first share here on forums so i decided to make a tutorial
That's fine, just doing a little constructive criticism for you to help you improve in the long run :P
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)