Creating a /sethp command -
DaleWestbrook - 16.06.2014
Hey guys excuse me if this tutorial is badly explained or wrong as it is my first tutorial and i thought i might try it out
So this tutorial will hopefully help you to create a /sethp command which will look like /sethp [playerid] [health]
You will need two includes for this command. The first include is
ZCMD - by Zeex (
https://sampforum.blast.hk/showthread.php?tid=91354)
and also
sscanf 2.8.1 - by Y_Less (
https://sampforum.blast.hk/showthread.php?tid=91354)
You will need to open up the files and put ZCMD in your include folder which can be accessed by going into your scripts folder then into pawno and you should see include and just add it there but while you are there add the sscanf include aswell. You will then have to add the sscanf plugins which is easy again just go to your server folder and open the folder named plugins and insert the file. Lastly open your server.cfg file and add the plugins which should look like
Then you need to add the includes into the script with two simple lines
PHP Code:
#include <ZCMD>
#include <sscanf2>
now lets get started:
navigate to where ever you want to write the command on your script. Also now with ZCMD the command format will not be under public OnPlayerCommandText(but it still can be) and the format of the command is now
PHP Code:
CMD:command(playerid,params[])
{
return 1;
}
or
PHP Code:
COMMAND:command(playerid,params[])
{
return 1;
}
Personally i use the first one as it is faster to type. now lets get to the actual command:
Your gonna wanna use a format for the command to type in so ill use CMD: which will make it
PHP Code:
CMD:sethp(playerid,params[])
{
return 1;
}
Were then gonna make it so only an admin can use this command so we will use IsPlayerAdmin so lets put it in the command
PHP Code:
CMD:sethp(playerid,params[])
{
if(IsPlayerAdmin(playerid)//You will need to use /rcon login in game to use the command or it wont work
return 1;
}
Got it so far?
Lets continue
Lets make it so the admin part actually does something so we need to open that part up like so:
PHP Code:
CMD:sethp(playerid,params[])
{
if(IsPlayerAdmin(playerid)//You will need to use /rcon login in game to use the command or it wont work
{
new Health;// This will help with the amount of health
new PID;// This is the target
if(sscanf(params, "ud", PID, Health)) return SendClientMessage(playerid, -1, "USAGE: /sethp [playerid] [health]");//This is where sscanf comes in we will show that they need to type the player and also the amount of health
if(!IsPlayerConnected(PID)) return SendClientMessage(playerid, -1, "Player is not connected");//This is just checking if the target is online (! means not so !IsPlayerConnected is player not connected)
SetPlayerHealth(PID, Health);// We are setting the health for the target
}//closing the bracket for is the player is an admin
else SendClientMessage(playerid, -1, "You are not an admin");//This will send the command user a message stating that s/he is not an admin (the -1 means white)
return 1;//Just returning the command
}//Closing the command and your done
This is what the command should end up looking like
PHP Code:
CMD:sethp(playerid,params[])
{
if(IsPlayerAdmin(playerid))
{
new Health;
new PID;
if(sscanf(params, "ud", PID, Health)) return SendClientMessage(playerid, -1, "USAGE: /sethp [playerid] [health]");
if(!IsPlayerConnected(PID)) return SendClientMessage(playerid, -1, "Player is not connected");
SetPlayerHealth(PID, Health);
}
else SendClientMessage(playerid, -1, "You are not authorized to use this command!");
return 1;
}
So that is it for this tutorial i hope i helped. Feel free to correct or even just ask questions.
Edit: Thank you to the guys that commented
Re: Creating a /sethp command -
Abagail - 16.06.2014
Why are you teaching scripters to use integers for this kind of command? Yes, it will work how-ever using floats would be far more effective, and useful. You even made the variable as a float how-ever you detect it as an integer with sscanf... Also, why are you using brackets to run one function of code?
Example:
Before:
Bad coding:
pawn Code:
else
{
SendClientMessage(playerid, -1, "You are not authorized to use this command!");
}
Good coding:
pawn Code:
else SendClientMessage(playerid, -1, "You are not authorized to use this command!");
Re: Creating a /sethp command -
NewerthRoleplay - 16.06.2014
Adding to what Abagail said you should also indent because the final command shouldn't look like:
pawn Code:
CMD:sethp(playerid,params[])
{
if(IsPlayerAdmin(playerid))
{
new Float:Health;
new PID;
if(sscanf(params, "ud", PID, Health)) return SendClientMessage(playerid, -1, "USAGE: /sethp [playerid] [health]");
if(!IsPlayerConnected(PID)) return SendClientMessage(playerid, -1, "Player is not connected");
SetPlayerHealth(PID, Health);
}
else
{
SendClientMessage(playerid, -1, "You are not authorized to use this command!");
}
return 1;
}
It should look like this with the proper indentation and with abagail's adjustment:
pawn Code:
CMD:sethp(playerid,params[])
{
if(IsPlayerAdmin(playerid))
{
new Float:Health;
new PID;
if(sscanf(params, "ud", PID, Health)) return SendClientMessage(playerid, -1, "USAGE: /sethp [playerid] [health]");
if(!IsPlayerConnected(PID)) return SendClientMessage(playerid, -1, "Player is not connected");
SetPlayerHealth(PID, Health);
}
else SendClientMessage(playerid, -1, "You are not authorized to use this command!");
return 1;
}
Re: Creating a /sethp command -
SKAzini - 17.06.2014
pawn Code:
CMD:sethp(playerid,params[])
{
if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "You are not authorized to use this command!");
new Float:Health, PID;
if(sscanf(params, "uf", PID, Health)) return SendClientMessage(playerid, -1, "USAGE: /sethp [playerid] [health]");
if(!IsPlayerConnected(PID)) return SendClientMessage(playerid, -1, "Player is not connected.");
SetPlayerHealth(PID, Health);
return 1;
}
:]
Re: Creating a /sethp command -
AndreiWow - 18.11.2014
Thanks!!