SA-MP Forums Archive
[Tutorial] /afk and /back. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] /afk and /back. (/showthread.php?tid=471383)



/afk and /back. - Kyance - 22.10.2013

"Content"
  • Introduction
"Title" says everything.
  • A "chat" about the script
It's "features"(Is it abusable etc..)
  • Tutorial itself
I will try to teach you how to make the script in this point.
It will contain:
zCMD, how to #include etc.
Color defines.
Script itself.
  • Full code
If you were too lazy to follow the tutorial, then there's the full script.

"Introduction"
So, today i will hopefully teach you how to create a pretty "advanced"( i guess ) /afk and /back system.
"The Chat"
Basically this /afk | /back script is like any other i guess, but people won't be able to spam /afk 2x or /back 2x.
"The Tutorial (0/3)"
"Finally", right?
Alright, first of all, you will need to get zCMD.
// https://sampforum.blast.hk/showthread.php?tid=91354 \\
That's the only Include you will need for this tutorial.
"#include <zcmd> (1/3) [Super basic]"
Alright, now that you've downloaded the zcmd.ini file, you will need to do the following:
1) Open your "Pawno"(Or how-ever you named the folder, where you have your pawno.exe, gamemode folder etc.
2) Inside of that, you will have "Pawno", open it, open "include" and drag your zcmd.ini file there.
3) Open up your pawno.exe, and open your gamemode.
Then type
pawn Code:
#include <zcmd>
This is how it looks for me.
pawn Code:
#include <a_samp>
#include <zcmd>
well, i have other includes too..
Alright, next thing.
"color #define (2/3) [Super basic]"
You will need these color defines, for this script.
COLOR_GREEN 0x33AA33AA
COLOR_ORANGE 0xFF9900AA
To define them, simply add this under your #include(s)
pawn Code:
#define COLOR_GREEN 0x33AA33AA
#define COLOR_ORANGE 0xFF9900AA
This is how it should look for you now.
pawn Code:
#include <a_samp>
#include <zcmd>
#define COLOR_GREEN 0x33AA33AA
#define COLOR_ORANGE 0xFF9900AA
Without the define, you will get an error in the script itself.

"The commands" (3/3) [?]"
Alright, now we will start scripting..
"/afk(3/3) [?]"
First of all, you will need to add this under your #include(s) and under your #define(s)
pawn Code:
new
    afk[ MAX_PLAYERS char ]
;
That will help your further in the script (Mostly to detect if the player is /AFK already / hasn't typed /AFK before /back".)
Alright, now that you have zCMD, you can't script in the "OnPlayerCommandText" callback.
Go to your scripts bottom, and paste/type this.
pawn Code:
CMD:afk(playerid, params[]) {
This is basically the "start" of everything.
When the player types "/afk" it will start triggering the command.

pawn Code:
new name[MAX_PLAYER_NAME], string[128];
    GetPlayerName(playerid, name, sizeof(name));
    if( afk{ playerid } ) SendClientMessage(playerid, COLOR_ORANGE, "[AFK] - You're already afk!");
new name and GetPlayerName will get the players name, who has typed the command (Will help in the other parts of script, for a SendClientMessageToAll)

pawn Code:
if( afk{ playerid } ) SendClientMessage(playerid, COLOR_ORANGE, "[AFK] - You're already afk!");
This will detect if the player is already afk, while typing the /afk command again.
This should avoid some spam.
But it won't avoid /back and /afk spam..

pawn Code:
else
    {
    afk{ playerid } = true;
else
...
...
If the player isn't AFK already, it will set his afk char to true.

pawn Code:
SetPlayerHealth(playerid, 99999);
    SetPlayerArmour(playerid, 99999);
    TogglePlayerControllable(playerid, 0);
Sets the Players health and the Players Armour to 99999, so he can't get killed.
(You can also use OnPlayerUpdate for this..)

pawn Code:
TogglePlayerControllable(playerid, 0);
This will make the player freeze, so he can't move.

pawn Code:
format(string, sizeof(string), "[AFK] - %s is now afk!", name);
    SendClientMessageToAll(COLOR_ORANGE, string);
    SendClientMessage(playerid, COLOR_ORANGE, "[AFK] - When you come back, use /back!");
format(string ... Will "format" a string (SendClientMessageToAll), with the message "%s(NAME) is now afk!
Without the format(string, you wouldn't be able to use %s.
And without %s, you would need to use "SendClientMessageToAll(COLOR_ORANGE, "Somebody went afk!");", which wouldn't be so nice.
The last "SendClientMessage" will tell the player, how to get back from the afk "position".

"/afk full-script(3/3) [?]"
pawn Code:
CMD:afk(playerid, params[]) {
    new name[MAX_PLAYER_NAME], string[128];
    GetPlayerName(playerid, name, sizeof(name));
    if( afk{ playerid } ) SendClientMessage(playerid, COLOR_ORANGE, "[AFK] - You're already afk!");
    else
    {
    afk{ playerid } = true;
    SetPlayerHealth(playerid, 99999);
    SetPlayerArmour(playerid, 99999);
    TogglePlayerControllable(playerid, 0);
    format(string, sizeof(string), "[AFK] - %s is now afk!", name);
    SendClientMessageToAll(COLOR_ORANGE, string);
    SendClientMessage(playerid, COLOR_ORANGE, "[AFK] - When you come back, use /back!");
    }
    return 1;
}
"/back command(3/3) [?]"
Now we will start working on the /back command.
pawn Code:
CMD:back(playerid, params[]) {
    if( !afk{ playerid } ) SendClientMessage(playerid, COLOR_ORANGE, "[BACK] - You aren't afk!");
if( !afk{ playerid } ) will detect if the player is NOT afk.
If the player is NOT afk, he will receive the mesasge "[BACK] - You aren't afk!"

pawn Code:
else
    {
    new name[MAX_PLAYER_NAME], string[128];
    GetPlayerName(playerid, name, sizeof(name));
else (If there were no "errors")
Again, will get the players name for a format (SendClientMessageToAll)

pawn Code:
SetPlayerHealth(playerid, 99);
    SetPlayerArmour(playerid, 99);
    TogglePlayerControllable(playerid, 1);
This will "restore" the players health/armour to 99 (I have a pretty "cheap" anti-cheat, that's why i set it to 99.)
pawn Code:
TogglePlayerControllable(playerid, 1);
Will unfreeze the player // Let the player move again.

pawn Code:
format(string, sizeof(string), "[BACK] - %s is now back!", name);
    SendClientMessageToAll(COLOR_GREEN, string);
    SendClientMessage(playerid, COLOR_GREEN, "[BACK] - Welcome back!");
    afk{ playerid } = false;
pawn Code:
format(string, sizeof(string), "[BACK] - %s is now back!", name);
This will format a strings message.
Again, this is needed for the %s.
pawn Code:
SendClientMessageToAll(COLOR_GREEN, string);
This will send the strings([BACK] - %s(NAME) is now back!) message to everyone.
pawn Code:
SendClientMessage(playerid, COLOR_GREEN, "[BACK] - Welcome back!");
This will send a message to the player(Saying Welcome back!)
pawn Code:
afk{ playerid } = false;
This will avoid people spamming /back.
It will say "You aren't afk!"
pawn Code:
if( !afk{ playerid } ) SendClientMessage(playerid, COLOR_ORANGE, "[BACK] - You aren't afk!");
Is the code that "helps" the
pawn Code:
afk{ playerid } = false;
.
"/back full-script(3/3) [?]"
pawn Code:
CMD:back(playerid, params[]) {
    if( !afk{ playerid } ) SendClientMessage(playerid, COLOR_ORANGE, "[BACK] - You aren't afk!");
    else
    {
    new name[MAX_PLAYER_NAME], string[128];
    GetPlayerName(playerid, name, sizeof(name));
    SetPlayerHealth(playerid, 99);
    SetPlayerArmour(playerid, 99);
    TogglePlayerControllable(playerid, 1);
    format(string, sizeof(string), "[BACK] - %s is now back!", name);
    SendClientMessageToAll(COLOR_GREEN, string);
    SendClientMessage(playerid, COLOR_GREEN, "[BACK] - Welcome back!");
    afk{ playerid } = false;
    }
    return 1;
}



Re: /afk and /back. - DarkLored - 22.10.2013

Good job kyance +reped


Re: /afk and /back. - David (Sabljak) - 23.10.2013

Maybe you can put some SetPlayerPos with saving old when typing /back because you can abuse this for god mode or something...


Re: /afk and /back. - Kyance - 24.10.2013

Quote:
Originally Posted by David (Sabljak)
View Post
Maybe you can put some SetPlayerPos with saving old when typing /back because you can abuse this for god mode or something...
Shouldn't really be "a problem", since it sets "TogglePlayerControllable(playerid, 0);"
Even if people can find a way how to abuse this, you can always trust your admins.
Well eh, not really sure if you can do it with the regular GetPlayerPos/SetPlayerPos, maybe..
Will try it out a bit later with OnPlayerUpdate and with if GetPlayerPos :]
----
EDIT:
----
1)
Added
pawn Code:
ResetPlayerWeapons(playerid);
in the /afk command.
And
pawn Code:
SetPlayerPos(playerid, 373.825653,-117.270904,1001.499511);
    SetPlayerInterior(playerid, 5);
Now the /afk command looks like this:

pawn Code:
CMD:afk(playerid, params[]) {
    new name[MAX_PLAYER_NAME], string[128];
    GetPlayerName(playerid, name, sizeof(name));
    if( afk{ playerid } ) SendClientMessage(playerid, COLOR_ORANGE, "[AFK] - You're already afk!");
    else
    {
    SetPlayerPos(playerid, 373.825653,-117.270904,1001.499511);
    SetPlayerInterior(playerid, 5);
    afk{ playerid } = true;
    SetPlayerHealth(playerid, 99999);
    SetPlayerArmour(playerid, 99999);
    ResetPlayerWeapons(playerid);
    TogglePlayerControllable(playerid, 0);
    format(string, sizeof(string), "[AFK] - %s is now afk!", name);
    SendClientMessageToAll(COLOR_ORANGE, string);
    SendClientMessage(playerid, COLOR_ORANGE, "[AFK] - When you come back, use /back!");
    }
    return 1;
}
OnPlayerUpdate:

pawn Code:
public OnPlayerUpdate(playerid)
{
if( afk { playerid }) {
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x,y,z);
    if(x != 373.825653 && y != -117.270904 && z != 1001.499511) //If a hacker will try to teleport to people.
    SetPlayerPos(playerid, 373.825653,-117.270904,1001.499511);//He will be set to this position.
    SetPlayerInterior(playerid, 5);
    }
    return 1;
}
You can change the pos and remove the Interior if you want/need to.
-----
EDIT NR. 2
-----
Modified the /back command..
pawn Code:
CMD:back(playerid, params[]) {
    if( !afk{ playerid } ) SendClientMessage(playerid, COLOR_ORANGE, "[BACK] - You aren't afk!");
    else
    {
    new name[MAX_PLAYER_NAME], string[128];
    GetPlayerName(playerid, name, sizeof(name));
    SetPlayerInterior(playerid, 0);
    SpawnPlayer(playerid); //Spawns the player, so the player isn't stuck in the shop interior.
    SetPlayerHealth(playerid, 99);
    SetPlayerArmour(playerid, 99);
    TogglePlayerControllable(playerid, 1);
    format(string, sizeof(string), "[BACK] - %s is now back!", name);
    SendClientMessageToAll(COLOR_GREEN, string);
    SendClientMessage(playerid, COLOR_GREEN, "[BACK] - Welcome back!");
    afk{ playerid } = false;
    }
    return 1;
}



Re: /afk and /back. - GreekOne - 25.10.2013

This is bad and you should feel bad!