[Tutorial] Making an easy /pay [playerid/name] [amount] system
#1

/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

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
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:
pawn Code:
dcmd_pay(playerid,params[])
{
    return 1;
}
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:
pawn Code:
if (sscanf(params, "ii", ID,amount)) return SendClientMessage(playerid, 0xff0000aa, "* Usage: /pay [playerid] [amount]");
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:
pawn Code:
static ID, amount;
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...

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))
this checks if the amount that the player has inserted in the second "i" is more than his own amount of money.

pawn Code:
return SendClientMessage(playerid, 0xff0000aa, "* You do not have enough money to pay that player!");
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:

pawn Code:
if (amount <= 0) return SendClientMessage(playerid, 0xff0000aa, "* You can't pay less than 1!");
under
pawn Code:
if (amount > GetPlayerMoney(playerid)) return SendClientMessage(playerid, 0xff0000aa, "* You do not have enough money to pay that player!");
so:
pawn Code:
if (amount <= 0)
this checks if the player has inserted an amount equal to 0 or less than 0
pawn Code:
return SendClientMessage(playerid, 0xff0000aa, "* You can't pay less than 1!");
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:
pawn Code:
if (playerid == ID) return SendClientMessage(playerid, 0xff0000aa, "* You can't pay yourselve!");
under our other if statements
pawn Code:
if(playerid == ID)
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:

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);
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:
pawn Code:
static ID, amount, name[MAX_PLAYERS], string7[200], name2[MAX_PLAYERS], string8[200];
pawn Code:
GetPlayerName(ID, name2, sizeof(name2));
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...

pawn Code:
format(string8,sizeof(string8),"{FFFF00}|- You have paid %s $%i -|",name2,amount);
    SendClientMessageToAll(COLOR_RED,string8);
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:
pawn Code:
SetPlayerMoney(ID, GetPlayerMoney(ID) +amount);
    SetPlayerMoney(playerid, GetPlayerMoney(playerid) -amount);
under the string things

pawn Code:
SetPlayerMoney(ID, GetPlayerMoney(ID) +amount);
this sets the player that has been paid's money to the amount the command user has entered...

pawn Code:
SetPlayerMoney(playerid, GetPlayerMoney(playerid) -amount);
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:

Code:
dcmd(pay,3,cmdtext);
and now compile your script, and it should work : )
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;
}
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!
Reply


Messages In This Thread
Making an easy /pay [playerid/name] [amount] system - by knackworst - 11.09.2011, 18:40
Re: Making an easy /pay [playerid/name] [amount] system - by Wesley221 - 11.09.2011, 18:51
Re: Making an easy /pay [playerid/name] [amount] system - by knackworst - 11.09.2011, 18:52
Re: Making an easy /pay [playerid/name] [amount] system - by Wesley221 - 11.09.2011, 18:53
Re: Making an easy /pay [playerid/name] [amount] system - by FireCat - 11.09.2011, 18:53
Re: Making an easy /pay [playerid/name] [amount] system - by knackworst - 11.09.2011, 18:54
Re: Making an easy /pay [playerid/name] [amount] system - by Prumpuz - 11.09.2011, 19:22
Re: Making an easy /pay [playerid/name] [amount] system - by knackworst - 11.09.2011, 19:39
Re: Making an easy /pay [playerid/name] [amount] system - by Prumpuz - 11.09.2011, 20:11
Re: Making an easy /pay [playerid/name] [amount] system - by knackworst - 11.09.2011, 20:13
Re: Making an easy /pay [playerid/name] [amount] system - by Davz*|*Criss - 12.09.2011, 11:53
Re: Making an easy /pay [playerid/name] [amount] system - by NickTaSpy - 25.10.2011, 17:47
Re: Making an easy /pay [playerid/name] [amount] system - by knackworst - 25.10.2011, 19:45
Re: Making an easy /pay [playerid/name] [amount] system - by Elka_Blazer - 25.10.2011, 19:46
Re: Making an easy /pay [playerid/name] [amount] system - by Kurvits - 29.10.2011, 18:03
Re: Making an easy /pay [playerid/name] [amount] system - by Kostas' - 03.11.2011, 14:14
Re: Making an easy /pay [playerid/name] [amount] system - by knackworst - 03.11.2011, 14:56
Re: Making an easy /pay [playerid/name] [amount] system - by Kostas' - 03.11.2011, 15:16
Re: Making an easy /pay [playerid/name] [amount] system - by Astralis - 19.11.2011, 09:27
Re: Making an easy /pay [playerid/name] [amount] system - by stylerof619 - 19.11.2011, 19:40
Re: Making an easy /pay [playerid/name] [amount] system - by knackworst - 20.11.2011, 11:07
Re: Making an easy /pay [playerid/name] [amount] system - by stylerof619 - 20.11.2011, 14:22
Re: Making an easy /pay [playerid/name] [amount] system - by .:Kaos:. - 20.11.2011, 14:35
Re: Making an easy /pay [playerid/name] [amount] system - by stylerof619 - 20.11.2011, 14:42

Forum Jump:


Users browsing this thread: 3 Guest(s)