[Tutorial] AFK/BACK Simple System. [YSI]
#1

Introduction

I made a simple AFK/BACK system for those who are noob in scripting and i used [YSI] as my saving system so hope you learn something from it and its my first time making a post on forums so hope you enjoy it Thanks.

Tutorial


Step 1:

First of all we need includes which will make the command work.

PHP Code:
/* Includes */
#include <a_samp>
#include <YSI\y_ini>
#include <zcmd> 
Step 2:

Second of all we need variables which we will use in command.

PHP Code:
/* Variables */
new AFKMode[MAX_PLAYERS]; 
Step 3:

Now we need command from which player will go afk and can come back from afk.

PHP Code:
/* Commands */
CMD:afk(playeridparams[])
{
    if (
AFKMode[playerid]) return SendClientMessage(playeridCOLOR_RED"You are already AFK."); //If player is already afk it will send a message to him saying you are already afk.
    
AFKMode[playerid] = 1//This will change the variable to 1.
    
new string[256], pname[MAX_PLAYER_NAME]; //New variables.
    
GetPlayerName(playeridpnamesizeof(string)); //This will get the player name.
    
TogglePlayerControllable(playerid0); //The player will not be able to move.
    
format(stringsizeof(string), "%s is now AFK."pname);
    
SendClientMessageToAll(COLOR_YELLOWstring); //This will send the message to all that the player is afk now.
    
return 1;
}
CMD:back(playeridparams[])
{
    if (!
AFKMode[playerid]) return SendClientMessage(playeridCOLOR_RED"You are not afk."); //If player is not afk it will send a message to him saying you are not afk.
    
AFKMode[playerid] = 0//This will change the variable to 0.
    
new string[256], pname[MAX_PLAYER_NAME]; //New variables.
    
GetPlayerName(playeridpnamesizeof(string)); //This will get the player name.
    
TogglePlayerControllable(playerid1); //The player will be able to move.
    
format(stringsizeof(string), "%s is now back from afk."pname);
    
SendClientMessageToAll(COLOR_YELLOWstring); //This will send the message to all that the player is afk now.
    
return 1;



So this was the tutorial which i made and i know i didn't explain that much good so sorry for that i will make good tutorial's soon so wait for them thanks for checking it out.
Reply
#2

“Spoonfeeding is not tutoring”, don't ever present ready snippets of code on a silver plate and call it a tutorial. Unless you present a full code analysis that is.

There are some notes to raise here though like string sizes “256” sounds horrifyingly big. Here's a cleaner version of your code:

PHP Code:
// A boolean array makes more sense in this context, a player is going to be
// either AFK (true) or not AFK (false).
new bool:AFKMode[MAX_PLAYERS];
// Hex-colors definitions.
#define COLOR_RED       0xFF000088
#define COLOR_YELLOW    0xFFFF0088
// We can omit the “params” arguments as it's not needed here.
CMD:afk(playerid)
{
    
// We test to see if AFKMode is equal to true, if so, then the player is already AFK, and thus we return the message
    // “You are already AFK.” and the code below it won't bother to execute.
    
if (AFKMode[playerid]) return SendClientMessage(playeridCOLOR_RED"You are already AFK.");
    
    
// Remember, readable variable names, and following a naming pattern makes it easier on the eye, in this case, it's camelCase.
    // afkMessage size: 14 + 1 (NUL character) + MAX_PLAYER_NAME + 1 (NUL character).
    // pName size: MAX_PLAYER_NAME + 1 (NUL character).
    
new afkMessage[15 MAX_PLAYER_NAME 1], pName[MAX_PLAYER_NAME 1];
    
// We set the AFKMode variable to true, meaning that the player is AFK.
    
AFKMode[playerid] = true;
    
// We freeze the player so that he or she cannot move or control their camera.
    // Passing false or 0 (negative values) as the second parameter acts the same.
    
TogglePlayerControllable(playeridfalse);
    
    
// We get the player's name and store it in the pName variable.
    // If the player's name is “player_killer_123”, that is going to be the value of pName.
    
GetPlayerName(playeridpNamesizeof(pName));
    
    
// We format a chunk of text that will result in something like “player_killer_123 is now AFK.”
    
format(afkMessagesizeof(afkMessage), "%s is now AFK."pName);
    
// We send the formatted message to all players, including the one going AFK.
    
SendClientMessageToAll(COLOR_YELLOWafkMessage);
    return 
1;

Reply
#3

you said 'tutorial'
but you released whole snippet
please explain it so others can understand by giving whole code they gonna copy only and they wont learn anything
so explain the whole code that why are we using that varaible what will hppen by using that variable
alot you have to explain
Reply
#4

Ooook, explain us why you used YSI here?
Reply
#5

I think there's like 10+ of these, try to create something more unique and not re-create something what has been released a lot but with only a few changes in the code.
Reply
#6

Ok ok, i will try to make a unique script thanks for commenting.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)