14.05.2012, 16:48
(
Последний раз редактировалось Saad_; 15.05.2012 в 14:45.
)
AFK System
A Tutorial on how to create an AFK system
Why did you make this ?A Tutorial on how to create an AFK system
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];
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
}
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
}
pawn Код:
PlayerInfo[playerid][AFK] = 0;
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;
}
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;
}
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;
}
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