Bug in this command
#1

Hello... i have bug here.

Код:
dcmd_givecash(playerid,params[])
{
  new tmp[256], idx;
  tmp = strtok(params,idx);
  if(!strlen(tmp))
  {
    SendClientMessage(playerid, COLOUR_RED, "Use: /givecash [playerid] [amount]");
    return true;
  }
  new pid = strval(tmp);
  if(!IsPlayerConnected(pid))
  {
    SendClientMessage(playerid, COLOUR_RED, "Incorrect playerid.");
    return true;
  }
  tmp = strtok(params,idx);
  if(!strlen(tmp))
  {
    SendClientMessage(playerid, COLOUR_RED, "Use: /givecash [playerid] [amount]");
    return true;
  }
  new amount = strval(tmp), string[256], string1[256], pname2[MAX_PLAYER_NAME];
  GetPlayerName(playerid, pname2, sizeof(pname2));
  format(string, sizeof(string), "%s sent you $%d", pname2, amount);
  format(string1, sizeof(string1), "You sent %s, $%d", pname2, amount);
  SendClientMessage(pid,COLOUR_GREEN, string);
  SendClientMessage(playerid,COLOUR_GREEN, string1);
  GivePlayerMoney(pid, amount);
  GivePlayerMoney(playerid, -amount);
  return true;
}
How i tyoe /givecash (id) 100 i send to player 100$ no? is works, but i type /givecash (id) -100 i steal to the other player 100$

How i can fixed this?

Help me please
Reply
#2

Use strsscanf, not strtok.
Reply
#3

Make an if statement and check if amount is > than 0.
Reply
#4

Here you go, a working code. Fixed also a couple of other small errors.
pawn Код:
dcmd_givecash(playerid,params[])
{
  new
    targetid,
    amount;

  if(sscanf(params, "ui", targetid, amount))
  {
    SendClientMessage(playerid, COLOUR_RED, "Use: /givecash [playerid] [amount]");
    return true;
  }
  if(!IsPlayerConnected(targetid))
  {
    SendClientMessage(playerid, COLOUR_RED, "Incorrect playerid.");
    return true;
  }
  if(amount < 1)
  {
    SendClientMessage(playerid, COLOUR_RED, "Invalid transaction amount");
    return 1;
  }
  new
    string[128],
    pname[MAX_PLAYER_NAME],
    pname2[MAX_PLAYER_NAME];

  GetPlayerName(playerid, pname, sizeof(pname));
  GetPlayerName(targetid, pname2, sizeof(pname2));

  format(string, sizeof(string), "%s sent you $%d", pname, amount);
  SendClientMessage(targetid,COLOUR_GREEN, string);

  format(string, sizeof(string), "You sent %s, $%d", pname2, amount);
  SendClientMessage(playerid,COLOUR_GREEN, string);
 
  GivePlayerMoney(targetid, amount);
  GivePlayerMoney(playerid, -amount);

  return true;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)