[GUIDE][Requires ZCMD][In-Depth] Admin duty cmd.
#1

Hello there! I'm ZTTVS, aka Spongey; today I decided I want to contribute to the SA-MP scripter community and make my first scripting guide! Now go easy on me, the only other guide I made is for my gaming community, and it's a guide that involves Teamspeak, not PAWN.

Anyways, enough with the horrible header, let's get going!

May I add this is on the newbie/intermediate side of scripting.

Credit to Audi_Quattrix for the original guide this is based off of.

Starting out

First of all, open your gamemode's script, and at the very top do this:
Code:
#define COLOUR_BLUE 0x0548FF
new adminDuty[MAX_PLAYERS];
In a very slight way, you could think of the #define tag as a char, or a string variable. You can set values to it, and setting it to 0x0548FF, sets it to the string "COLOUR_BLUE". Scripters use the #define tag a lot of the time to set colours to things, you can do this by using something like colorpicker.com and replacing "0548FF". Keep the 0x, however, I'll explain this later.... That took way too much about the #define tag.. Let's continue.


The next thing this little line of code does is create a new variable called "adminDuty", and it uses the MAX_PLAYERS loop. The loop is too hard to explain in my own words, but a good reference is THIS HERE. It's from probably the finest resource for scripters, the SA-MP wiki. Then of course, at the very end it ends the statement with a semicolon; as usual.

Next is to go to "OnPlayerConnect" on your script. Under that, you want to place this line of code:

Code:
adminDuty[playerid] = 0;
This line of code sets it so when the player spawns, the player isn't automatically spawned on admin duty.

It first declares that we're using adminDuty for the player, that's what [playerid] is for. Without it, it does absolutely nothing.

You want to do this for "OnPlayerDisconnect" as well. Otherwise it might confuse the system, as it did mine. Same reasons.

NOW, THE MOMENT YOU HAVE ALL BEEN WAITING FOR... THE COMMAND.

Note: Now that took us longer than I thought to explain all that. Now, of course, this post will be edited so it's more understandable/gives more information.

On to the command. This command (in my opinion) is a bit intermediate, and I'll be explaining it as I'm talking to an intermediate PAWN scripter. Now I know that I contradicted myself by saying that, as I wrote that this is for complete newbies, but just... Bare with me if you can.

First of all, this command is going to need the ZCMD include, which is quite popular, so I'm sure you can find it somewhere in the include section of this forum. If you don't know how to include/don't know what an include is, read the SA-MP wiki a few times as it's a very basic concept that should be one of the first things you learn when you script.

Now where was I? That's right, the command. So... Post this in your script somewhere:

Code:
CMD:adminduty(playerid)
{
    if(PlayerInfo[playerid][pAdmin] >=1 || IsPlayerAdmin(playerid))
    {
        if (adminDuty[playerid] == 0)
        {
            new string[128];
            SetPlayerHealth(playerid,999999); 
            SetPlayerColor(playerid,COLOUR_BLUE);
            SetPlayerSkin(playerid,294);
            format(string,sizeof(string), "%s is now on Duty!",GetPlayerName(playerid));
            SendClientMessageToAll(COLOUR_BLUE,string);
            SendClientMessage(playerid,COLOUR_BLUE,"You are now on duty!");
            adminDuty[playerid] = 1;
        }
        else if (adminDuty[playerid] == 1)
        {
            new string[128];
            SetPlayerHealth(playerid,100);
            format(string,sizeof(string)," %s is now off Duty!",GetPlayerName(playerid));
            SendClientMessageToAll(COLOUR_BLUE,string);
            SendClientMessage(playerid, COLOUR_BLUE,"You are now off duty!");
            adminDuty[playerid] = 0;
        }
    }
    else
        SendClientMessage(playerid,COLOUR_BLUE,"You're not an admin!");
    return 1;
}
Now that's the command's code, if you look at it with a decent amount of knowledge, it's pretty simple. However, for the people who don't EXACTLY understand the code, I'll explain it. Briefly, we don't need a bunch of sentences explaining each and every keyword.

I start off by declaring the commands name, and the parameters. ZCMD makes it easy for this, as all I need to do is write "CMD", "command", or just "cmd". So let's see what we got here?

Code:
CMD:adminduty
Looks spectacular. But obviously, it's just a little statement that does absolutely nothing. Infact, it's barely even a statement as PAWN can't even understand it. Can we fix this? Duh. It's pretty simple, you have to add your parameters and squiggly brackets!! Our parameters will be (playerid), as we're going to be effecting the player, not a car, not a... I don't know, light post? Anyways, here comes the squiggly brackets that complete it. Now let's see how our command is doing, shall we?

Code:
CMD:adminduty(playerid)
{

}
Looks like Jesus Christ himself, dunnit?

Now we have to make the system find out if the player is an admin when he executes the command. We do this by using an "if" statement(Tip: Things like "new", "if", "else", etc are all case sensitive, so when writing them, make sure they're all lower-case. ). It declares if the player is ACTUALLY an administrator execute whatever's in the squiggly brackets. The statement that says this is obviously:

Code:
if(PlayerInfo[playerid][pAdmin] >=1 || IsPlayerAdmin(playerid))
Now the "In-Def" type of explanation(in my own terms):
If the player's record/log thing/whatever(PlayerInfo[playerid][pAdmin]) says he's an admin is set to true(>=1), or Is the player admin(IsPlayerAdmin) execute a bunch of code inside of these squiggly brackets.

Next is this

Code:
if (adminDuty[playerid] == 0)
I honestly think this is self-explanatory. But for those who don't understand, here it is:

The system checks if the player is on admin duty(if), and he is not(adminDuty[playerid] == 0). And since it's and if statement, it has to execute the code inside ITS squiggly brackets once again.

Then this:

Code:
  new string[128];
            SetPlayerHealth(playerid,999999); 
            SetPlayerColor(playerid,COLOUR_BLUE);
            SetPlayerSkin(playerid,294);
            format(string,sizeof(string), "%s is now on Duty!",GetPlayerName(playerid));
            SendClientMessageToAll(COLOUR_BLUE,string);
            SendClientMessage(playerid,COLOUR_BLUE,"You are now on duty!");
            adminDuty[playerid] = 1;
I'm not going to explain all of it, as most of it is irrelevant; however if you don't know you could use the SA-MP wiki, which I'll reference plenty of times if I continue to make guides. There is something that I think would be necessary to explain. That's this:
Code:
adminDuty[playerid] = 1;
This simply sets the players admin duty status to true.

In depth:
Sets the players admin duty status(adminDuty[playerid]) to true(= 1).

NEXT!:

Code:
else if (adminDuty[playerid] == 1)
This creates an "else if" statement, another thing I don't have time to explain at the moment, so once again I'll refer to the SA-MP wiki.

I can explain this though:
Code:
adminDuty[playerid] == 1
Pretty much, the code says "If the player is on admin duty(adminDuty[playerid] == 1) do this(squiggly brackets):".

I hope you're starting to get the hang of this.

Then here's what's inside of those squiggly brackets:

Code:
  new string[128];
            SetPlayerHealth(playerid,100);
            format(string,sizeof(string)," %s is now off Duty!",GetPlayerNameEx(playerid));
            SendClientMessageToAll(COLOUR_BLUE,string);
            SendClientMessage(playerid, COLOUR_BLUE,"You are now off duty!");
            aDuty[playerid] = 0;
Same deal as before, no point in explaining most of it. But at the end, there's this:
Code:
aDuty[playerid] = 0;
This simply sets the players status on being on admin duty to 0(which is FALSE.).

The final part:

Code:
    else
        SendClientMessage(playerid,COLOUR_BLUE,"You're not an admin!");
This creates an else statement, meaning if all else are false do this, which is the SendClientMessage.

This final part is self explanatory, if the player isn't an admin, send 'em a message.

Final words

First of all, I want to thank you for reading my first guide, it means a lot. I know it's not the greatest, but hey, watcha' gonna do? I hope you liked it, and I understand I went a bit lazy towards the end, you can blame me for getting tired :P

Anyways, good luck on your many adventures of scripting!

Reply
#2

Is it possible an admin can move this into the tutorial section? Thanks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)