[QUESTION] About OnPlayerCommandText
#1

Hello guys I have a question. I wrote this code which heals specific player...
pawn Код:
if (strcmp(cmd, "/heal", true) == 0) {
        new tmp[128];
        tmp = strtok(cmdtext, idx); // find string after " " (space) and store it in tmp
 
        if (strlen(tmp) == 0) {
            return SendClientMessage(playerid, 0xFFFFFFFF, "USAGE: /heal [playerid]");
        }      
       
        new player = strval(tmp); // get integer value from string (0 if the string is not numeric)
       
        new messageStr[256];
        if (!IsPlayerConnected(player)) {
            format(messageStr, sizeof(messageStr), "Player with id %i does not exist!", player);
            return SendClientMessage(playerid, 0xFFFFFFFF, messageStr);
        } else {
            format(messageStr, sizeof(messageStr), "Player with id %i has been healed.", player);
            SetPlayerHealth(player, 100.0);
            return SendClientMessage(playerid, 0xFFFFFFFF, messageStr);
        }  
    }
So what happens is if i write /heal asdf it heals player with id 0 and I don't want that to happen. strval() returns 0 if the string is not numeric and that is the problem. Any ideas?
Reply
#2

pawn Код:
isnumeric(const string[])
{
    for (new i = 0, j = strlen(string); i < j; i++)
    {
        if (string[i] > '9' || string[i] < '0') return 0;
    }
    return 1;
}
pawn Код:
if (!isnumeric(tmp)) return SendClientMessage(playerid, -1, "USAGE: /heal [playerid]");
You really should use zcmd (or y_commands) and sscanf though, it makes creating commands much faster and easier.
Reply
#3

Thanks dude. I`ll give it a try
Reply
#4

Works! Thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)