SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 06/01/2013, 09:25 PM   #1
JustinAn
Huge Clucker
 
JustinAn's Avatar
 
Join Date: Apr 2012
Location: stfu
Posts: 237
Reputation: 28
Default Creating a /givemoney command (ZCMD & SSCANF)



Creating A /givemoney Command




Introduction
Hello guys, and today I'll be showing my tutorial on creating a /givemoney command using sscanf, zcmd. (Most useful in my opinion).




Getting Started

First, your going to want to define the include 'ZCMD', a fast command processor. Next you're going to want to define a include called 'sscanf2'.

pawn Code:
#include <ZCMD>
#include <sscanf2>
Now, here's a note for all of you. When you're doing ZCMD commands, do not, I mean never place the commands in any call backs, like in OnPlayerCommandText especially.

Here are the links to downloading the includes.

- sscanf2
- zcmd

You will also need to define some colors.
Here is defining the color 'COLOR_LIGHTBLUE'.
pawn Code:
#define COLOR_LIGHTBLUE     0x33CCFFAA

Now, let's start with the commands.

The structure in starting an command is this, there are several ways into doing this. But I will just be only explaining the latest structure that is formed.
pawn Code:
CMD:commandhere(playerid, params[])
{
    return 1;
}

So here is this code, the command starter. The structure to start a command.
The params[] is the parameters string, playerid is an ID of the player who sends the command.
pawn Code:
CMD:commandhere(playerid, params[]) //Structure to begin an command.

We also will need a stock, called GetName.
This defines the name, it will get the playerid's name. And will replace the '_' underscore into nothing so FirstName LastName. Which is mostly useful for RolePlay servers.
pawn Code:
stock GetName(playerid)
{
    new
        name[24];
    GetPlayerName(playerid, name, sizeof(name));
    strreplace(name, '_', ' ');
    return name;
}

Creating the command

Now, the command we're doing today is the /givemoney command which was said in the Introduction section.

Here is the code, and I will explain the whole code after this.
pawn Code:
CMD:givemoney(playerid, params[])
{
    if(IsPlayerAdmin(playerid))
    {
        new string[128], targetid, money;
        if(sscanf(params, "ud", targetid, money)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /givemoney [playerid] [money]");

        if(IsPlayerConnected(targetid))
        {
            GivePlayerMoney(targetid, money);
            format(string, sizeof(string), "* You have set %s's cash to an amount of $%d.",GetName(targetid),money);
            SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
        }
    }
    else
    {
        SendClientMessage(playerid, -1, "You are not authorized to use that command!");
    }
    return 1;
}

Now, this was already explained above the code. But the command is 'givemoney'.
pawn Code:
CMD:givemoney(playerid, params[])

Next is this code, this code is checking if the player who sent the command is logged in RCON admin, '/rcon login [your pass]', that type of thing.
pawn Code:
if(IsPlayerAdmin(playerid))


Now, this code is defining a string, the target who sent the command and the amount of money.
pawn Code:
new string[128], targetid, money;


This is the part where we use sscanf, now. The 'if(sscanf(params' is the part which is also like 'isnull'. The "ud", these are the specifiers. 'u' is the playerid that the person who sent the command places the playerid who he/she wants to give the money to, the 'd' is the integer, which is for the money where the person who sends the command to give a certain amount to the player that he/she wants to. 'return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /givemoney [playerid] [money]");' Is showing if they typed in a wrong parameter, it will guide them to showing them how to use the command correctly, just like 'isnull'. It will send the person a message, in white and saying the usage on how to use the command.
pawn Code:
if(sscanf(params, "ud", targetid, money)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /givemoney [playerid] [money]");'

Here is a list of the specifiers.
Specifiers from ******.
Code:
Specifier(s)			Name				Example values
	i, d			Integer				1, 42, -10
	c			Character			a, o, *
	l			Logical				true, false
	b			Binary				01001, 0b1100
	h, x			Hex				1A, 0x23
	o			Octal				045 12
	n			Number				42, 0b010, 0xAC, 045
	f			Float				0.7, -99.5
	g			IEEE Float			0.7, -99.5, INFINITY, -INFINITY, NAN, NAN_E
	u			User name/id (bots and players)	******, 0
	q			Bot name/id			ShopBot, 27
	r			Player name/id			******, 42
Here is the code where it checks if the ID who the player sent the command to give the money to that if he/she is connected to the server.
pawn Code:
if(IsPlayerConnected(targetid))

This code is giving the target who the player wants to the amount of money that the player executed in the command.
pawn Code:
GivePlayerMoney(targetid, money);

Now, this part is where we're using our string defined at the top of the command.
We're formatting the string, saying '* You have set %s's cash to an amount of $%d.' Now, the %s is the player's name, that person who executed the command that he wants to send the cash to. Now the %d is the integer, where the amount of money that the person who sended the certain amount of cash. After that, it will show 'GetName', this get's the name of the person who is receiving the cash. The money is the amount of money where the person who sends the amount of cash to, to the person he/she wants.
pawn Code:
format(string, sizeof(string), "* You have set %s's cash to an amount of $%d.",GetName(targetid),money);

This is sending the message to the player who executed the commands in the COLOR_LIGHTBLUE color, and sending the string that I explained above.
pawn Code:
SendClientMessage(playerid, COLOR_LIGHTBLUE, string);

This part it where it's connected to the 'IsPlayerAdmin(playerid))'. So if the player's not an RCON admin/logged in.
It will send the player a message in white which is '-1' by the way, saying 'You are not authorized to use that command!'
pawn Code:
else
{
    SendClientMessage(playerid, -1, "You are not authorized to use that command!");
}



Conclusion
And that's it for today in my tutorial on creating the /givemoney command ONLY for logged in RCON admins.

Credits to...
- ****** for creating such a wonderful sscanf include.
- Zeex for creating the wonderful ZCMD include.
- And me for creating this tutorial.

__________________

Last edited by JustinAn; 19/07/2013 at 11:53 PM.
JustinAn is offline   Reply With Quote
Old 06/01/2013, 10:31 PM   #2
Glad2BeHere
Gangsta
 
Glad2BeHere's Avatar
 
Join Date: Oct 2012
Posts: 759
Reputation: 136
Default Re: Creating a /givemoney command (ZCMD & SSCANF)

Give player Cash? ur failing @ this.... Its GivePlayerMoney *cough*
for ppl that wanna use giveplayercash use this anti money system
http://forum.sa-mp.com/showthread.php?t=71136
Glad2BeHere is offline   Reply With Quote
Old 06/01/2013, 11:20 PM   #3
JustinAn
Huge Clucker
 
JustinAn's Avatar
 
Join Date: Apr 2012
Location: stfu
Posts: 237
Reputation: 28
Default Re: Creating a /givemoney command (ZCMD & SSCANF)

Quote:
Originally Posted by Glad2BeHere View Post
Give player Cash? ur failing @ this.... Its GivePlayerMoney *cough*
for ppl that wanna use giveplayercash use this anti money system
http://forum.sa-mp.com/showthread.php?t=71136
Oh yeah, sorry. Was in my script (: I'll fix that.

EDIT: And please, I don't like people calling me failing at stuff.
Please...
__________________
JustinAn is offline   Reply With Quote
Old 07/01/2013, 08:32 PM   #4
JustinAn
Huge Clucker
 
JustinAn's Avatar
 
Join Date: Apr 2012
Location: stfu
Posts: 237
Reputation: 28
Default Re: Creating a /givemoney command (ZCMD & SSCANF)

Quote:
Originally Posted by ****** View Post
And so you shouldn't, if people insult you again feel free to report the posts and the mod team will deal with them.
Thanks ******, I appreciate your help (:
__________________
JustinAn is offline   Reply With Quote
Old 07/01/2013, 08:45 PM   #5
RedCrossER
Huge Clucker
 
Join Date: Nov 2012
Posts: 274
Reputation: 8
Default Re: Creating a /givemoney command (ZCMD & SSCANF)

Anyway good work. Report those who ruin your work.
RedCrossER is offline   Reply With Quote
Old 07/01/2013, 09:30 PM   #6
JustinAn
Huge Clucker
 
JustinAn's Avatar
 
Join Date: Apr 2012
Location: stfu
Posts: 237
Reputation: 28
Default Re: Creating a /givemoney command (ZCMD & SSCANF)

Quote:
Originally Posted by RedCrossER View Post
Anyway good work. Report those who ruin your work.
Thanks, I appreciate it alot!
__________________
JustinAn is offline   Reply With Quote
Old 28/03/2013, 11:25 AM   #7
Fernado Samuel
Gangsta
 
Join Date: Oct 2012
Location: United States, Ohio
Posts: 665
Reputation: 32
Default Re: Creating a /givemoney command (ZCMD & SSCANF)

Good work
Fernado Samuel is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a sscanf command to Zcmd Sarra Scripting Help 12 22/09/2012 01:04 PM
kick command with zcmd and sscanf DanTzi Scripting Help 3 19/06/2012 03:47 PM
Goto command with sscanf, zcmd PlayerID Help Archive 5 30/07/2011 10:47 PM
how to make this command to zcmd and sscanf? xir Help Archive 1 21/02/2011 01:13 PM
need help with /pm command using zcmd & sscanf OldDirtyBastard Help Archive 3 26/01/2011 07:59 PM


All times are GMT. The time now is 12:51 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.