11.09.2011, 18:40
(
Last edited by knackworst; 11/09/2011 at 07:41 PM.
)
/pay commmand tutorial:
What does it do??
It's basically a command to give other players money, u can only give the player money, if you have the amount of money that u want to give...
So you cant do this: /pay misterplayer 1000000 if you only have got 1$
My goal, is that after you have read this tutorial, you can create much commands like these yourself : )
Ok So let's get started:
Step 1:
First of all, we are going to use DCMD
we only need a define
now, I have no idea what the hell that means, I think there are only a few people who know what it means (you don't need to know what it means to make commands using DCMD...)...
anyways, this is just one define u need in your script to use dcmd.
Ok, so now, u can use DCMD.
We will also need scanf.
Wich can be downloaded here: https://sampforum.blast.hk/showthread.php?tid=120356
just follow the instructions to install and download
Step 2:
Now we are going to make the command...
put this anywhere in your script:
this is the command, change Pay in to whatever you want to name your command...
ok, so now we are going to add statics.
statics, are almost the same as new, except that static can only be used in one command, and new in your entire script...
static ID;
ok, so this is a static to make something that will track, who you wanna pay
(there are more statics ofcourse, but i'll add them only when necesairy so I won't confuse you)
now add this under the first bracket:
ok so this are the params, the first "i" stands for the player u wanna pay, and the second "i" for the amount.
this is what makes this type of commands different from the normal commands, u used to make.
Now we can use everything that a player insert after the /pay [something here] [something here]
ofcourse for the amount we need to make a new static, so your static line should look like this now:
ok, so the return SendClientMessage(playerid,...) is just a message that u will get when you are not using the command correctly...
Now we are going to make limitations, so the player cannot give more money than he has, and the player cannot pay theirselves...
ok so add this under the if(scanff(params...
this checks if the amount that the player has inserted in the second "i" is more than his own amount of money.
This sends a message that you are trying to give the player more than you have...
Next limitation we are going to create is that the player cannot give the other player less than 1$
so add this:
under
so:
this checks if the player has inserted an amount equal to 0 or less than 0
This sends a message that the player has tried to give 0 or less than 0
Next Limitation: now let's make sure that the player cannot give money to themselves...
so add this:
under our other if statements
this means when the player who types the command = playerid executes the command on himself...
so the playerid is always the commnad user
the ID is always the one u do the command on.
and then the return SendClientMessage is the message u get when you wnna pay yourself again...
Next We are going to create something that gives the player a message when he/she pays someone and a message to the player who gets the money...
for that add this:
under our if statements.
Ok so here we are going to use strings, to tell the player how much he gave and who he gave it too.
Same for the player who gets the money
because we are going to use strings, we need to make new statics.
so your static line should look like this now:
this will insert the name of the player you gave the money too in our string.
NOTE: It's important that it's GetPlayerName(ID
if you do this: GetPlayerName(playerid
you will get the name of the player who does the command...
Ok, so here we choose the text for the player, at the end of this format you can see name2,amount
this will insert the string "name2" in the %s, so ingame it will give: You have Paid playeryoupaid
The amount, will insert the amount that the player wants to pay into the %i
we did not have to create a string for the amount, because its already used it in the scanf params
and then the SendClientMessageToAll, will just be the format we have created
NOTE: You might see COLOR_RED, if you haven't define it in your script yet you will get an error: undefined symbol: COLOR_RED
just change the COLOR_RED into a color code or into a red color define you have already defined in your script...
Ok, now last we need to give the player the money...
for that add:
under the string things
this sets the player that has been paid's money to the amount the command user has entered...
this will set the command user money minus the amount he entered, if you don't do this the player who uses the command's money will remain the same...
Step 3:
now, the last step:
Go to your OnPlayerCommandText callback and add this:
and now compile your script, and it should work : )
if you get errors, please reply!
Final look:
I hope I have learned you how to use commands like these: /command [something here]
in this tutorial : )
if you want to make these commands with more params just add another "i" or "d" or "u" or a different scanf param, and u can use it
Thanks!
What does it do??
It's basically a command to give other players money, u can only give the player money, if you have the amount of money that u want to give...
So you cant do this: /pay misterplayer 1000000 if you only have got 1$
My goal, is that after you have read this tutorial, you can create much commands like these yourself : )
Ok So let's get started:
Step 1:
First of all, we are going to use DCMD
we only need a define
pawn Code:
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
anyways, this is just one define u need in your script to use dcmd.
Ok, so now, u can use DCMD.
We will also need scanf.
Wich can be downloaded here: https://sampforum.blast.hk/showthread.php?tid=120356
just follow the instructions to install and download
Step 2:
Now we are going to make the command...
put this anywhere in your script:
pawn Code:
dcmd_pay(playerid,params[])
{
return 1;
}
ok, so now we are going to add statics.
statics, are almost the same as new, except that static can only be used in one command, and new in your entire script...
static ID;
ok, so this is a static to make something that will track, who you wanna pay
(there are more statics ofcourse, but i'll add them only when necesairy so I won't confuse you)
now add this under the first bracket:
pawn Code:
if (sscanf(params, "ii", ID,amount)) return SendClientMessage(playerid, 0xff0000aa, "* Usage: /pay [playerid] [amount]");
this is what makes this type of commands different from the normal commands, u used to make.
Now we can use everything that a player insert after the /pay [something here] [something here]
ofcourse for the amount we need to make a new static, so your static line should look like this now:
pawn Code:
static ID, amount;
Now we are going to make limitations, so the player cannot give more money than he has, and the player cannot pay theirselves...
ok so add this under the if(scanff(params...
pawn Code:
if (amount > GetPlayerMoney(playerid)) return SendClientMessage(playerid, 0xff0000aa, "* You do not have enough money to pay that player!");
pawn Code:
if(amount > GetPlayerMoney(playerid))
pawn Code:
return SendClientMessage(playerid, 0xff0000aa, "* You do not have enough money to pay that player!");
Next limitation we are going to create is that the player cannot give the other player less than 1$
so add this:
pawn Code:
if (amount <= 0) return SendClientMessage(playerid, 0xff0000aa, "* You can't pay less than 1!");
pawn Code:
if (amount > GetPlayerMoney(playerid)) return SendClientMessage(playerid, 0xff0000aa, "* You do not have enough money to pay that player!");
pawn Code:
if (amount <= 0)
pawn Code:
return SendClientMessage(playerid, 0xff0000aa, "* You can't pay less than 1!");
Next Limitation: now let's make sure that the player cannot give money to themselves...
so add this:
pawn Code:
if (playerid == ID) return SendClientMessage(playerid, 0xff0000aa, "* You can't pay yourselve!");
pawn Code:
if(playerid == ID)
so the playerid is always the commnad user
the ID is always the one u do the command on.
and then the return SendClientMessage is the message u get when you wnna pay yourself again...
Next We are going to create something that gives the player a message when he/she pays someone and a message to the player who gets the money...
for that add this:
pawn Code:
GetPlayerName(ID, name2, sizeof(name2));
format(string8,sizeof(string8),"{FFFF00}|- You have paid %s $%i -|",name2,amount);
SendClientMessageToAll(COLOR_RED,string8);
GetPlayerName(playerid, name, sizeof(name));
format(string7,sizeof(string7),"{FFFF00}* %s(%d) Has paid you: $%i",name,playerid,amount);
SendClientMessage(ID,COLOR_RED,string7);
Ok so here we are going to use strings, to tell the player how much he gave and who he gave it too.
Same for the player who gets the money
because we are going to use strings, we need to make new statics.
so your static line should look like this now:
pawn Code:
static ID, amount, name[MAX_PLAYERS], string7[200], name2[MAX_PLAYERS], string8[200];
pawn Code:
GetPlayerName(ID, name2, sizeof(name2));
NOTE: It's important that it's GetPlayerName(ID
if you do this: GetPlayerName(playerid
you will get the name of the player who does the command...
pawn Code:
format(string8,sizeof(string8),"{FFFF00}|- You have paid %s $%i -|",name2,amount);
SendClientMessageToAll(COLOR_RED,string8);
this will insert the string "name2" in the %s, so ingame it will give: You have Paid playeryoupaid
The amount, will insert the amount that the player wants to pay into the %i
we did not have to create a string for the amount, because its already used it in the scanf params
and then the SendClientMessageToAll, will just be the format we have created
NOTE: You might see COLOR_RED, if you haven't define it in your script yet you will get an error: undefined symbol: COLOR_RED
just change the COLOR_RED into a color code or into a red color define you have already defined in your script...
Ok, now last we need to give the player the money...
for that add:
pawn Code:
SetPlayerMoney(ID, GetPlayerMoney(ID) +amount);
SetPlayerMoney(playerid, GetPlayerMoney(playerid) -amount);
pawn Code:
SetPlayerMoney(ID, GetPlayerMoney(ID) +amount);
pawn Code:
SetPlayerMoney(playerid, GetPlayerMoney(playerid) -amount);
Step 3:
now, the last step:
Go to your OnPlayerCommandText callback and add this:
Code:
dcmd(pay,3,cmdtext);
if you get errors, please reply!
Final look:
pawn Code:
dcmd_pay(playerid,params[])
{
static ID, amount, name[MAX_PLAYERS], string7[200], name2[MAX_PLAYERS], string8[200];
if (sscanf(params, "ii", ID,amount)) return SendClientMessage(playerid, 0xff0000aa, "* Usage: /pay [playerid/name] [amount]");
if (amount > GetPlayerMoney(playerid)) return SendClientMessage(playerid, 0xff0000aa, "* You do not have enough money to pay that player!");
if (amount <= 0) return SendClientMessage(playerid, 0xff0000aa, "* You can't pay less than 1!");
if (playerid == ID) return SendClientMessage(playerid, 0xff0000aa, "* You can't pay yourselve!");
GetPlayerName(ID, name2, sizeof(name2));
format(string8,sizeof(string8),"{FFFF00}|- You have paid %s $%i -|",name2,amount);
SendClientMessageToAll(COLOR_RED,string8);
GetPlayerName(playerid, name, sizeof(name));
format(string7,sizeof(string7),"{FFFF00}* %s(%d) Has paid you: $%i",name,playerid,amount);
SendClientMessage(ID,COLOR_RED,string7);
GivePlayerMoney(ID,amount);
GivePlayerMoney(playerid,-amount);
return 1;
}
in this tutorial : )
if you want to make these commands with more params just add another "i" or "d" or "u" or a different scanf param, and u can use it
Thanks!