16.10.2011, 10:02
I'm using zcmd and sscanf. What would be the easiest way to make a simple /pay [playerid] [amount] Command?
CMD:pay(playerid, params[])
{
new P_NAME[ MAX_PLAYER_NAME ], T_NAME[ MAX_PLAYER_NAME ], sZ[ 128 ];
if( sscanf ( params, "ui", params[ 0 ], params[ 1 ] ) ) return SendClientMessage( playerid -1, #Syntax /pay name ammount );
if( params[ 0 ] == INVALID_PLAYER_ID ) return SendClientMessage( playerid, -1, #Player offline );
if( params[ 1 ] > GetPlayerMoney ( playerid ) ) return SendClientMessage( playerid, -1, #Invalid ammount );
GetPlayerName( playerid, P_NAME, MAX_PLAYER_NAME ); GetPlayerName( params[ 0 ], T_NAME, MAX_PLAYER_NAME );
format( sZ, sizeof ( sZ ), "You have received $%d from %s ", params[ 1 ], P_NAME );
SendClientMessage( params[ 0 ], sZ );
format( sZ, sizeof ( sZ ), "You have payed $%d to %s ", params[ 1 ], T_NAME );
SendClientMessage( playerid, sZ );
GivePlayerMoney ( playerid, - params[ 1 ] );
GivePlayerMoney ( params[ 0 ], params[ 1 ] );
return true;
}
CMD:pay(playerid, params[])
{
new Target, Amount;
if( !sscanf(params, "ui", Target, Amount) )
{
if( Amount < 5 ) return SendClientMessage(playerid, -1, "Minimum amount: 5"); // if amount is lower as 5
if( Target == INVALID_PLAYER_ID ) return SendClientMessage(playerid, -1, "Invalid playerid!"); // if target is an invalid playerid
if( Target == playerid ) return SendClientMessage(playerid, -1, "You cant pay yourself."); // if youre the target
if( GetPlayerMoney(playerid) < Amount ) return SendClientMessage(playerid, -1, "You dont have that amount of cash."); // if you dont have enough money you wanted to send to the player
GivePlayerMoney(playerid, -Amount); // removing the money from you
GivePlayerMoney(Target, Amount); // adding the money to you
} else return SendClientMessage(playerid, -1, "Usage: /pay <playerid> <money>");
return 1;
}
@smit/wesley: He asked how to do it, not for the code. People don't learn from code, but from experience.
Simply said, retrieve the target id and the amount from the parameters, then check if the target is actually connected. If he isn't, send an error message to the player. If he is, check if the amount of money is not more than the player actually has (You can't give out $1000 if you've only got $200, for example). Same story: If the player wants to give more than he has, send an error. If not, remove that amount from the player id and give it to the target. Now it's up to you to code it. (Please, do try and code it yourself) |