/makeadmin command
#1

So i made a /makeadmin command, whenever i execute the command on a non-admin player it just sends me "USAGE: /makeadmin (playerID) (Level)"

I know that there will be a mistake before i scripted this because i don't know how to make a command that is more advance than i usually do.

Ignore the loose indentation.

PHP код:
CMD:makeadmin(playeridparams[])
{
    new 
targetidAdminLeveltname[MAX_PLAYER_NAME], pname[MAX_PLAYER_NAME], str[128]; 
    if(
pInfo[playerid][pAdmin] < 5) return SendClientMessage(playeridGREY"You are not authorized to use that command.");
    if(
sscanf(params,"ui"targetid)) return SendClientMessage(playeridGREY"USAGE: /makeadmin (playerID) (Level)");
    if(
AdminLevel 10) return SendClientMessage(playeridGREY"LEVELS AVAILABLE 1-10!");
    if(!
IsPlayerConnected(targetid)) return SendClientMessage(playeridGREY"ERROR: Player is not connected!");
    
GetPlayerName(playeridtnamesizeof(tname));
    
GetPlayerName(targetidpnamesizeof(pname));
    if(
AdminLevel == 0)
    {
        
format(strsizeof(str),"Administrator %s has set your admin level to %d, Congratulations!!"tnameAdminLevel);
        
SendClientMessage(targetidREDstr);
        
format(strsizeof(str),"You have set %s's Admin level to %d"pnameAdminLevel);
        
SendClientMessage(playeridREDstr);
        
pInfo[targetid][pAdmin] = AdminLevel;
     }
     return 
1;

Reply
#2

You're not assigning a variable to the second specifier in sscanf making it undetectable whether a second parameter was entered so it will always say that the second parameter was not entered:

PHP код:
if(sscanf(params,"ui"targetidAdminLevel)) 
And unusable in the rest of the code.
Reply
#3

Quote:
Originally Posted by AndySedeyn
Посмотреть сообщение
You're not assigning a variable to the second specifier in sscanf making it undetectable whether a second parameter was entered so it will always say that the second parameter was not entered:

PHP код:
if(sscanf(params,"ui"targetidAdminLevel)) 
And unusable in the rest of the code.
Still, didn't work.

EDIT: If okay, can you make it a unique one? Like changing it, making it better.
Reply
#4

Yes. Allow me to explain too what I altered.

I prioritize checks. A priority check in admin commands is the check whether or not the player using the command is authorized to use the command.

PHP код:
    if(pInfo[playerid][Admin] < 5)
        return 
SendClientMessage(playeridGREY"You are not authorized to use that command."); 
I then create the variables that are used first in the command. You should always try to delay the declaration of variables and prioritize them. The ones used first are created first and the ones used last are declared last:
PHP код:
    new
        
targetid,
        
AdminLevel
    

I use them in the sscanf function:

PHP код:
    if(sscanf(params"ui"targetidAdminLevel))
        return 
SendClientMessage(playeridGREY"USAGE: /makeadmin (playerid) (level)"); 
I don't know what version of sscanf you are using, but I seem to remember that there was a bug with the 'u' specifier in (one of the / a) previous version(s). If that's the case and you don't include NPC's in 'targetid' then you can use the 'r' specifier instead (and that might as well been your issue).

I once again prioritize checks according to the specifiers in the sscanf function. The first specifier is the one responsible for the playerid/name, so I check whether or not the player specified is valid first:

PHP код:
    if(!IsPlayerConnected(targetid))
        return 
SendClientMessage(playeridGREY"ERROR: player is not conencted."); 
The second specifier is the one for the admin level, so I check if the specified admin level is less than 10 and greater than 0 (since you want level to start from 1):
PHP код:
    if(10 AdminLevel 1)
        return 
SendClientMessage(playeridGREY"LEVELS available: 1 to 10."); 
And then we get to the point where we format our strings and assign the admin level to the player's variable. We first retrieve the names and thus we create the tname and pname variables first:
PHP код:
    new
        
tname[MAX_PLAYER_NAME],
        
pname[MAX_PLAYER_NAME]
    ; 
We retrieve the names:
PHP код:
    GetPlayerName(playeridpnamesizeof(pname));
    
GetPlayerName(targetidtnamesizeof(tname)); 
Now we can format the strings and I create the variable holding the formatted strings now too:
PHP код:
    new
        
str[101]
    ;
    
format(strsizeof(str), "Administrator %s has set your admin level to %d, %s"pnameAdminLevel, (AdminLevel pInfo[playerid][Admin]) ? ("congratulations on your promotion!") : ("improve yourself where possible!"));
    
SendClientMessage(targetidREDstr);
    
format(strsizeof(str), "You have set %s\'s admin level to %d"tnameAdminLevel);
    
SendClientMessage(playeridREDstr); 
And last but not least, we set the target's admin variable:
PHP код:
pInfo[targetid][Admin] = AdminLevel
Perhaps another part of my version that requires an explanation is this:
PHP код:
    format(strsizeof(str), "Administrator %s has set your admin level to %d, %s"pnameAdminLevel, (AdminLevel pInfo[playerid][Admin]) ? ("congratulations on your promotion!") : ("improve yourself where possible!")); 
You might ask yourself, what in earth is:
PHP код:
(AdminLevel pInfo[playerid][Admin]) ? ("congratulations on your promotion!") : ("improve yourself where possible!"
That's called the ternary operator and takes three arguments (hence the name ternary): https://en.wikipedia.org/wiki/%3F:

A way to describe what this exactly does is by replacing it by what it replaces (if-else):
PHP код:
if(AdminLevel pInfo[playerid][Admin]) {
    
format(strsizeof(str), "Administrator %s has set your admin level to %d, congratulations on your promotion!"pnameAdminLevel);
}
else {
    
format(strsizeof(str), "Administrator %s has set your admin level to %d, improve yourself where possible!"pnameAdminLevel);

You can basically see the ternary operator as such:
PHP код:
 condition true false 
If your 'true' and your 'false' are string values, then they are required to be put in between brackets:
PHP код:
condition ? ("true") : ("false"
Here's the full command:
PHP код:
CMD:makeadmin(playeridparams[]) {
    if(
pInfo[playerid][Admin] < 5)
        return 
SendClientMessage(playeridGREY"You are not authorized to use that command.");
    new
        
targetid,
        
AdminLevel
    
;
    if(
sscanf(params"ui"targetidAdminLevel))
        return 
SendClientMessage(playeridGREY"USAGE: /makeadmin (playerid) (level)");
    if(!
IsPlayerConnected(targetid))
        return 
SendClientMessage(playeridGREY"ERROR: player is not conencted.");
    if(
AdminLevel 10)
        return 
SendClientMessage(playeridGREY"LEVELS available: 1 to 10.");
    new
        
tname[MAX_PLAYER_NAME],
        
pname[MAX_PLAYER_NAME]
    ;
    
GetPlayerName(playeridpnamesizeof(pname));
    
GetPlayerName(targetidtnamesizeof(tname));
    new
        
str[101]
    ;
    
format(strsizeof(str), "Administrator %s has set your admin level to %d, %s"pnameAdminLevel, (AdminLevel pInfo[playerid][Admin]) ? ("congratulations on your promotion!") : ("improve yourself where possible!"));
    
SendClientMessage(targetidREDstr);
    
format(strsizeof(str), "You have set %s\'s admin level to %d"tnameAdminLevel);
    
SendClientMessage(playeridREDstr);
    
pInfo[targetid][Admin] = AdminLevel;
    return 
true;

Reply
#5

Didn't work -_- I don't know why, i guess because of my sscanf? :/
Reply
#6

Quote:
Originally Posted by iCurse
Посмотреть сообщение
Didn't work -_- I don't know why, i guess because of my sscanf? :/
Have you read any of the explanation that I put along with it?:

Quote:
Originally Posted by myself
I don't know what version of sscanf you are using, but I seem to remember that there was a bug with the 'u' specifier in (one of the / a) previous version(s). If that's the case and you don't include NPC's in 'targetid' then you can use the 'r' specifier instead (and that might as well been your issue).
I'm using sscanf2 and the 'u' specifier works perfectly.
Reply
#7

Never mind bro, it worked i downloaded the sscanf2 and worked fine. +rep bro ^_^ Thank you so much for the help, and i didn't understand ternary operator if okay can i remove it, if it's gonna cause something not good to my command then i won't. THANK YOU SO MUCH FOR THE HELP!! +REP...
Reply
#8

Quote:
Originally Posted by iCurse
Посмотреть сообщение
Never mind bro, it worked i downloaded the sscanf2 and worked fine. +rep bro ^_^ Thank you so much for the help, and i didn't understand ternary operator if okay can i remove it, if it's gonna cause something not good to my command then i won't. THANK YOU SO MUCH FOR THE HELP!! +REP...
Sure. You can replace it with an if-statement instead as I've shown in the example:
PHP код:
if(AdminLevel pInfo[playerid][Admin]) { 
    
format(strsizeof(str), "Administrator %s has set your admin level to %d, congratulations on your promotion!"pnameAdminLevel); 

else { 
    
format(strsizeof(str), "Administrator %s has set your admin level to %d, improve yourself where possible!"pnameAdminLevel); 

Reply
#9

Do i have to pick one of those?
PHP код:
format(strsizeof(str), "Administrator %s has set your admin level to %d, congratulations on your promotion!"pnameAdminLevel); 
PHP код:
format(strsizeof(str), "Administrator %s has set your admin level to %d, improve yourself where possible!"pnameAdminLevel); 
I don't get it.

And can you explain what "else" is? It would really be helpful to me to start being a scripter.
Reply
#10

@iCurse, the difference in these two lines above that the first one is promotion and second one demotion if I got it right. Correct me if I'm wrong.
Reply
#11

Also, how can i make this command be able to be by RCONs and Level 6 admins above?
Reply
#12

Quote:
Originally Posted by KevinExec
View Post
@iCurse, the difference in these two lines above that the first one is promotion and second one demotion if I got it right. Correct me if I'm wrong.
Yes it is, thanks for that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)