[Tutorial] How to make a basic /setlevel command for your server
#1

Introduction

Hello there fellow SA-MP members!I've decided to make a simple /setlevel or /makeadmin command to explain to those who don't understand it.I've looked up some tutorials but they were not explain properly (imo that is) so I've decided to make this tutorial.This is my first tutorial so I'll try to do my best and try to explain as much as I can.

Basics

For this command to work you'll need a Login-Register System Y_ini, which there are a lot of tutorials for that.
Second thing you'll need are these plugins
zcmd: https://sampforum.blast.hk/showthread.php?tid=91354
sscanf: https://sampforum.blast.hk/showthread.php?tid=570927

Once you have these plugins included a long with the LR System Y_ini you're good to go!


Command

Open up a new pawn script or use your own and lets get started.First off you need to define some colors or again you can use your own, I'll include these colors to make it as simple as possible
Code:
#define COLOR_GREEN 0x33AA33AA
#define COLOR_RED 0xAA3333AA
Next, find an empty space where you'll want your command to be.When you did that, now we need to decide what to call our command, I'll go with "/setlevel" because it's simple but again you can call it however you want.

Code:
CMD:setlevel(playerid, params[])
{
	return 1;
}
Now we need to define the things we wan't.

Code:
new id,level,msg[128],msg2[128],name[MAX_PLAYER_NAME];
Ok.Now I'll explain what we just defined since I feel you already lost me.
The id, represents the player who we wan't to make admin.The level is the admin level which we want to set, first of all I didn't want to include a string so I added these messages (msg and msg2) and last but not least the name is the player name whom we are setting the level

Now we will add

Code:
if(IsPlayerAdmin(playerid))
To check if the player is logged in rcon, if hes not it will display a message I will add at the end of the script.
Now lets see what will happen when the player enters the command.Put this next in the script.

Code:
if(sscanf(params,"ud",id,level))return SendClientMessage(playerid, COLOR_RED,"Usage: /setlevel [player id] [level]");
So when a player enters "/setlevel" it will display this message.
Now we wan't a message to display when you've entered an incorrect or invalid player id.

Code:
if(id == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_RED, "That player is not connected.");
Next off we want to decide how many admin levels we'll be having.

Code:
else if(level > 5)return SendClientMessage(playerid, COLOR_RED,"Admin levels are between 1 and 5!");
I added max. 5 levels but you can put any number you want.So if you've entered for e.g "/setlevel 0 6" it will display the message that the admin levels are between 1 and 5.

Now let's see what will happen when we use the command.First off add this

Code:
else
{
	
}
So now we will be putting a code that will display the command.

Code:
pInfo[id][Adminlevel] = level;
By adding this to the code we are actually setting the players admin level.Before we go any further this "pInfo and Adminlevel" depend on what you've included in your enum.Yours might be different so keep an eye on that.

So now that we actually added what the command will do here is where those msg's we included come.

Code:
format(msg,sizeof(msg),"Your admin level has been set to %d.",level);
This line will tell the player we've gave admin to the info that his admin level has been set.
Under this line put

Code:
SendClientMessage(id,COLOR_GREEN,msg);
This is always need to put under the format message.

Now we need to do is get the player name by adding this

Code:
GetPlayerName(id,name,sizeof(name));
Now here is where the second msg will come.What the second msg does, is informs you that you've set someones admin level to something.

Code:
format(msg2,sizeof(msg2),"You have set %s's admn level to %d.",name,level);
And of course we need

Code:
SendClientMessage(playerid,COLOR_GREEN,msg2);
But this time it's not "id", it's "playerid".Why?Well because it's informing you not the other player.

Now for the ending of the command I told that we were gonna display a message if the player isn't logged in rcon.
Add another "else"

Code:
}
    else
    {
And under it add this

Code:
SendClientMessage(playerid, COLOR_RED,">>Error<< You have to be logged in rcon to use this command!");
	}
	return 1;
When you've finished the command it should look like this

Code:
CMD:setlevel(playerid, params[])
{
	new id,level,msg[128],msg2[128],name[MAX_PLAYER_NAME];
	if(IsPlayerAdmin(playerid))
	{
	    if(sscanf(params,"ud",id,level))return SendClientMessage(playerid, COLOR_RED,"Usage: /setlevel [player id] [level]");
	    if(id == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_RED, "That player is not connected.");
	    else if(level > 5)return SendClientMessage(playerid, COLOR_RED,"Admin levels are between 1 and 5!");
	    else
	    {
	        pInfo[id][Adminlevel] = level;
	        format(msg,sizeof(msg),"Your admin level has been set to %d.",level);
	        SendClientMessage(id,COLOR_GREEN,msg);
			GetPlayerName(id,name,sizeof(name));
			format(msg2,sizeof(msg2),"You have set %s's admn level to %d.",name,level);
			SendClientMessage(playerid,COLOR_GREEN,msg2);
	    }
    }
    else
    {
        SendClientMessage(playerid, COLOR_RED,">>Error<< You have to be logged in rcon to use this command!");
	}
	return 1;
}
I didn't want to add any comments to the script since you'd probably delete it and it would be a waste of my and your time but everything is explained here.If you had problems with the command feel free to post about it in the commands.I know this tutorial isn't the best but since it's my first hah .I'll maybe try doing more of these command if you guys want.


Note:Have in mind that if you are new to zcmd or pawn DO NOT try this command!
Reply
#2

Code:
CMD:setlevel(playerid, params[])
{
	if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, COLOR_RED,">>Error<< You have to be logged in rcon to use this command!");
	else if(sscanf(params,"ud",params[0],params[1]))return SendClientMessage(playerid, COLOR_RED,"Usage: /setlevel [player id] [level]");
	else if(id == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_RED, "That player is not connected.");
	else if(level > 5) return SendClientMessage(playerid, COLOR_RED,"Admin levels are between 1 and 5!");
        new msg[50+MAX_PLAYER_NAME],name[MAX_PLAYER_NAME];
	pInfo[params[0]][Adminlevel] = params[1];
	format(msg,sizeof(msg),"Your admin level has been set to %d.",params[1]);
	SendClientMessage(params[0],COLOR_GREEN,msg);
	GetPlayerName(params[0],name,sizeof(name));
	format(msg,sizeof(msg),"You have set %s's admin level to %d.",name,params[1]);
	SendClientMessage(playerid,COLOR_GREEN,msg);
	return 1;
}
Reply
#3

It would be better-looking without those else if/else statements, especially when you read something to add this:
pawn Code:
}
    else
    {
Anyway, some things to change:

- Declare only 1 string and re-format it, no need for two.
- Add a minimum level check.
- Declare only "id" and "level" at the top, why would we want to declare unusable variables when an error is returned? Place the strings before setting the level.

Last, explain what the "u" and "i" specifiers are in the sscanf.
Reply
#4

Quote:
Originally Posted by Konstantinos
View Post
It would be better-looking without those else if/else statements, especially when you read something to add this:
pawn Code:
}
    else
    {
Anyway, some things to change:

- Declare only 1 string and re-format it, no need for two.
- Add a minimum level check.
- Declare only "id" and "level" at the top, why would we want to declare unusable variables when an error is returned? Place the strings before setting the level.

Last, explain what the "u" and "i" specifiers are in the sscanf.
Thanks for the feedback!I'll be sure to update the tutorial.
Reply
#5

Shouldn't we also check if the entered level is less that 0. 'd' specifiers are integers. So technically, They can set their level to -1.
Reply
#6

Seems more clean, basically added everything Konstantinos wanted you to change.
Code:
COMMAND:setlevel(playerid, params[]) {
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "{FF0000}You have to be logged in as rcon to use this command.");
    new target, level;
    if(sscanf(params, "ud", target, level)) return SendClientMessage(playerid, -1, "{FF0000}Usage: /setlevel [Player] [Level]");
    if(!IsPlayerConnected(target)) return SendClientMessage(playerid, -1, "{FF0000}This player is not connected.");
    if(level < 0 || level > 5) return SendClientMessage(playerid, -1, "{FF0000}Invalid level, please make sure this is between 0 and 5.");
    new string[144], name[24]; GetPlayerName(target, name, sizeof(name));
    format(string, sizeof(string), "{FF0000}Your admin level has been set to %d.", level);
    SendClientMessage(target, -1, string);
    format(string, sizeof(string), "{FF0000}You have set the admin level of %s to %d.", name, level);
    SendClientMessage(playerid, -1, string);
    pInfo[target][Adminlevel] = level;
    return 1;
}
Reply
#7

"Clean" is a matter of perception. Personally I don't like it when there's hardly any white-space and everything is pretty much crammed in as tight a space as possible. But that's an entirely different subject for discussion.
Reply
#8

Quote:
Originally Posted by Konstantinos
View Post
- Declare only 1 string and re-format it, no need for two.
Plus, you shouldn't even declare a name array (Sometimes it's necessary though)
You can do this instead:
Code:
new string[74];
GetPlayerName(playerid, string, 24);
format(string, sizeof (string), "Name: %s", string);
Reply
#9

Quote:
Originally Posted by Vince
View Post
"Clean" is a matter of perception. Personally I don't like it when there's hardly any white-space and everything is pretty much crammed in as tight a space as possible. But that's an entirely different subject for discussion.
"Seems"
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)