[Tutorial] This is how you create a /setadmin command
#1

Hi,

I've receantly noticed alot of people asking how they can set somebody admin level through dini, yini, and mysql.

Im going to show you how you can do that by using MYSQL and SSCANF and ZCMD.

Lets begin:

Step 1:
First of all lets start of with a blank pawno file, and include the neccesary includes which we are going to use. If you don't have these, its required you download the, (MySQL, sscanf, ZCMD)

pawn Код:
#include <a_samp>
#include <sscanf2>
#include <a_mysql>
#include <zcmd>
Step 2:
Now we can begin to create the command, we first need to create the shell of the command by doing the following. You can either choose to use COMMAND: or you can use CMD: I usuaully tend to use CMD: just because its shorter and quicker to type.

pawn Код:
CMD:setadmin(playerid, params[])
{

}
Step 3:
We can now begin to work on the body, now lets add some simple variables that we can use in the command

pawn Код:
CMD:setadmin(playerid, params[])
{
       new iD, gMessage[250], pName[24], pVictim[24], gQuery[250], Level;
}
The reason that we've used 'iD' is so we can use that variable to point towards the player that we want to use the command on. 'gMessage' basicly means the string that we are going to format. You can notice I've used pName, and pVictim, 'pName' is the variable I'm going to be setting the playerid as, and 'pVictim' is the other players name that im going to be setting. The 'gQuery' is the going to be formatted when we insert/update the new data. The variable 'Level' is what the players level is going to be set as.

Step 4:

Since all of the variables have been added, we can begin to create the if, and else if statements. We do this so we can check the requirements of the playerid, and other id.

pawn Код:
CMD:setadmin(playerid, params[])
{
       new iD, gMessage[250], pName[24], pVictim[24], gQuery[250], Level;
       if(sscanf(params, "ui", iD, Level)) return SendClientMessage(playerid, 0xFF0000FF, "Usage: /setadmin [playerid] [level]");
       else if(iD == playerid) return SendClientMessage(playerid, 0xFF0000FF, "You cannot use that command on yourself.");
       else if(!IsPlayerConnected(iD)) return SendClientMessage(playerid, 0xFF0000FF, "That player is not online.");
       else
       {


       }
       return 1;
}
The first if statement is checking wether the player has entered the right arguements, we've used the 'u' as the playerid, and we've used the 'i' as an integer for the Level. The second and third lines are checking if the player is the current player, and we are checking if that person is online or not.
[/pawn]

Step 5:

We are now going to add the mysql, by formatting a string, then calling the string with a mysql function. First of we need to get both of the players names for future use. The function we are going to use is the mysql_query(); This query's the string we formatted.

pawn Код:
CMD:setadmin(playerid, params[])
{
       new iD, gMessage[250], pName[24], pVictim[24], gQuery[250], Level;
       if(sscanf(params, "ui", iD, Level)) return SendClientMessage(playerid, 0xFF0000FF, "Usage: /setadmin [playerid] [level]");
       else if(iD == playerid) return SendClientMessage(playerid, 0xFF0000FF, "You cannot use that command on yourself.");
       else if(!IsPlayerConnected(iD)) return SendClientMessage(playerid, 0xFF0000FF, "That player is not online.");
       else
       {
               GetPlayerName(playerid, pName, sizeof(pName));
               GetPlayerName(iD, pVictim, sizeof(pVictim));
               format(gQuery, sizeof(gQuery), "UPDATE Accounts SET Admin = '%i' WHERE Username = '%s'", Level, gVictim);
               mysql_query(gQuery);
       }
       return 1;
}
Step 6:

We can now format the messages which are going to be sent out the both playerid, and the other id.

pawn Код:
CMD:setadmin(playerid, params[])
{
       new iD, gMessage[250], pName[24], pVictim[24], gQuery[250], Level;
       if(sscanf(params, "ui", iD, Level)) return SendClientMessage(playerid, 0xFF0000FF, "Usage: /setadmin [playerid] [level]");
       else if(iD == playerid) return SendClientMessage(playerid, 0xFF0000FF, "You cannot use that command on yourself.");
       else if(!IsPlayerConnected(iD)) return SendClientMessage(playerid, 0xFF0000FF, "That player is not online.");
       else
       {
               GetPlayerName(playerid, pName, sizeof(pName));
               GetPlayerName(iD, pVictim, sizeof(pVictim));
               format(gQuery, sizeof(gQuery), "UPDATE Accounts SET Admin = '%i' WHERE Username = '%s'", Level, gVictim);
               mysql_query(gQuery);

               format(gMessage, sizeof(gMessage), "* You have set %s has a level %i admin.", gVictim, Level);
               SendClientMessage(playerid, 0xFFFFFFFF, gMessage);
               
               format(gMessage, sizeof(gMessage), "* You have been set as a level %i admin.", Level);
               SendClientMessage(iD, 0xFFFFFFFF, gMessage);
       }
       return 1;
}
The first formatted string is sent to the playerid telling them who they have set as an admin, and what level they have set. The second formatted string is sent to the iD, showing them that they have been set as an admin, and what level they have been set.

Finished:

The command is now finished, you may also want to add some validations to check wether the person is an admin. You could also set them through a variable as an admin so it would instantly allow them to use admin commands. I hope this helped you, I know it isn't a very difficult command/tutorial but atleast its helping some.

Please give me reputation if you feel I deserved it.
Reply
#2

Very Nice Thank you But
Код:
else if(iD == playerid) return SendClientMessage(playerid, 0xFF0000FF, "You cannot use that command on yourself.");
then how you are gonna set your admin level ?
Reply
#3

Nice, very good explained!
Quote:
Originally Posted by [D]ry[D]esert
Посмотреть сообщение
Very Nice Thank you But
Код:
else if(iD == playerid) return SendClientMessage(playerid, 0xFF0000FF, "You cannot use that command on yourself.");
then how you are gonna set your admin level ?
Open up the table, and change it?
Reply
#4

The query I made opens up the table, and it sets the admin row to what ever level they chose at the players name.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)