[Tutorial] How to create a simple AFK system
#1

Hello guys ! In this short tutorial, I will talk about a simple AFK system which you can make it better if you want by adding a few lines to it. Let's start with our includes then !

At the top of our script there must be

pawn Код:
#include <a_samp>
You can add more includes if you need other includes for your other systems. But a_samp will be enough for our simple AFK system. So let's start ! After of

pawn Код:
#include <a_samp>
Add

pawn Код:
new isAFK[MAX_PLAYERS]; // We will edit this soon to tell the server whether a player is AFK or not, this is just a definition before telling so.
So it shall look like this:

pawn Код:
#include <a_samp>
new isAFK[MAX_PLAYERS]; // We will edit this  soon to tell the server whether a player is AFK or not, this is just a definition before telling so.
Next, we are going to define string. Below of

pawn Код:
new isAFK[MAX_PLAYERS];
Add this

pawn Код:
new string[128]; // This tells the script my text will be max. 128 bits, you can make it larger or lower for your needs...
So our top of the script shall look like this now:

pawn Код:
#include <a_samp>
new isAFK[MAX_PLAYERS]; // We will edit this soon to tell the server whether a player is AFK or not, this is just a definition before telling so.
new string[128]; // This tells the script my text will be max. 128 bits, you can make it larger or lower for your needs ...
So we are done with the top of our script. Hope i explained well.. Now we are going to play with OnPlayerConnect. Search for

pawn Код:
public OnPlayerConnect
in your pawno and add this under of it

pawn Код:
isAFK[playerid] = 0; // Setting player's AFK state to 0(false) to avoid bugs
So it shall look like this now:

pawn Код:
public OnPlayerConnect(blablabla)
{
isAFK[playerid] = 0; // Setting player's AFK state to 0(false) to avoid bugs on their connection period
return 1;
}
Now we are ready with everything, let's make our conmand then !

Search for

pawn Код:
public OnPlayerCommandText
This time. And add this under of it

pawn Код:
if(strcmp("/afk", cmdtext, true, 10) == 0)
{
This means if a player types /afk in game... So let's continue our code.

pawn Код:
if(strcmp("/afk", cmdtext, true, 10) == 0)
{
new Name[MAX_PLAYER_NAME]; // Defining the Player's Name
GetPlayerName(playerid, Name, sizeof(Name));
if  (isAFK[playerid] == 0) // if the player is NOT AFK
{
isAFK[playerid] = 1; // Turning his state to AFK mode
format( string, sizeof(string), "%s is AFK !", Name); // Formatting the message to be sended
SendClientMessageToAll(yourcolorhere , string); // Sends message to everyone to inform he is AFK, put a color in yourcolorhere part of this code to make it work.
}
else { // if the player is already AFK
isAFK[playerid] = 0; // Turning his state to NO-AFK mode
format( string, sizeof(string), "%s is back!", Name); // Formatting the message to be sended
SendClientMessageToAll(yourcolorhere, string); // Sends message to inform he is back , put a color in yourcolorhere part of this code to make it work.
}
return 1;
}
Sorry if there is any mistakes or grammatical mistakes, i am in phone plus i am sleepy right now. Hope this helps, feel free to ask anywhere you didnt understand or looking for a support to make this script bigger ... Also tell me if there is any bug please. Have fun !
Reply
#2

Looks very nice! Good tutorial
Reply
#3

Thank you !
Reply
#4

Thanks, this really help me so much!
Reply
#5

Simple, nice one mane
Reply
#6

Nice tutorial, can be useful for newbies. Though, you shall tab so they learn it too and become easier for them to script. Here is the command example:
Which one seems better?
This?
pawn Код:
if(strcmp("/afk", cmdtext, true, 10) == 0)
{
new Name[MAX_PLAYER_NAME]; // Defining the Player's Name
GetPlayerName(playerid, Name, sizeof(Name));
if  (isAFK[playerid] == 0) // if the player is NOT AFK
{
isAFK[playerid] = 1; // Turning his state to AFK mode
format( string, sizeof(string), "%s is AFK !", Name); // Formatting the message to be sended
SendClientMessageToAll(yourcolorhere , string); // Sends message to everyone to inform he is AFK, put a color in yourcolorhere part of this code to make it work.
}
else { // if the player is already AFK
isAFK[playerid] = 0; // Turning his state to NO-AFK mode
format( string, sizeof(string), "%s is back!", Name); // Formatting the message to be sended
SendClientMessageToAll(yourcolorhere, string); // Sends message to inform he is back , put a color in yourcolorhere part of this code to make it work.
}
return 1;
}
Or this?
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(strcmp("/afk", cmdtext, true, 10) == 0)
    {
        new Name[MAX_PLAYER_NAME]; // Defining the Player's Name
        GetPlayerName(playerid, Name, sizeof(Name));
        if  (isAFK[playerid] == 0) // if the player is NOT AFK
        {
            isAFK[playerid] = 1; // Turning his state to AFK mode
            format( string, sizeof(string), "%s is AFK !", Name); // Formatting the message to be sended
            SendClientMessageToAll(-1 , string); // Sends message to everyone to inform he is AFK, put a color in yourcolorhere part of this code to make it work.
        }
        else { // if the player is already AFK
            isAFK[playerid] = 0; // Turning his state to NO-AFK mode
            format( string, sizeof(string), "%s is back!", Name); // Formatting the message to be sended
            SendClientMessageToAll(-1, string); // Sends message to inform he is back , put a color in yourcolorhere part of this code to make it work.
        }
        return 1;
    }
    return 0;
}
Reply
#7

Quote:
Originally Posted by Necip
Посмотреть сообщение
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(strcmp("/afk", cmdtext, true, 10) == 0)
    {
        new Name[MAX_PLAYER_NAME]; // Defining the Player's Name
        GetPlayerName(playerid, Name, sizeof(Name));
        if  (isAFK[playerid] == 0) // if the player is NOT AFK
        {
            isAFK[playerid] = 1; // Turning his state to AFK mode
            format( string, sizeof(string), "%s is AFK !", Name); // Formatting the message to be sended
            SendClientMessageToAll(-1 , string); // Sends message to everyone to inform he is AFK, put a color in yourcolorhere part of this code to make it work.
        }
        else { // if the player is already AFK
            isAFK[playerid] = 0; // Turning his state to NO-AFK mode
            format( string, sizeof(string), "%s is back!", Name); // Formatting the message to be sended
            SendClientMessageToAll(-1, string); // Sends message to inform he is back , put a color in yourcolorhere part of this code to make it work.
        }
        return 1;
    }
    return 1;
}
The last should be "return 0;".

Quote:
Originally Posted by Rufio
Посмотреть сообщение
pawn Код:
if(strcmp("/afk", cmdtext, true, 10) == 0)
"/afk" has 4 lenght, not 10. Well, the lenght is option so it's better not to use it at all if it's gonna be incorrect or use it with the correct lenght.
Reply
#8

Quote:
Originally Posted by _Zeus
Посмотреть сообщение
The last should be "return 0;".



"/afk" has 4 lenght, not 10. Well, the lenght is option so it's better not to use it at all if it's gonna be incorrect or use it with the correct lenght.
Totally forgot that, as I use ZCMD all the time :P

Thanks anyways, edited.
Reply
#9

Quote:
Originally Posted by Necip
Посмотреть сообщение
Totally forgot that, as I use ZCMD all the time :P

Thanks anyways, edited.
If you use ZCMD,why not teach the new people zcmd! its easy to learn and since you are just showing them the code anyway,it will be simple,plus its more tidy with zcmd -_- *facepalm*
Reply
#10

Nice, but too long.

Top:

Код:
new message[128], bool:AFK[MAX_PLAYERS], name[MAX_PLAYER_NAME];

OnPlayerCommandText:

Код:
if(!strcmp(cmdtext, "/AFK", true))
{
    AFK[playerid] = !AFK[playerid];
    SetPlayerVirtualWorld(playerid, AFK[playerid]?playerid+1:0);
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    format(message, sizeof(message), AFK[playerid]?("%s is AFK!"):("%s isn't AFK"), name);
    return SendClientMessageToAll(-1, message);
}
*Write in the forum.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)