[QUESTION] About OnPlayerCommandText - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: [QUESTION] About OnPlayerCommandText (
/showthread.php?tid=534385)
[QUESTION] About OnPlayerCommandText -
vakhtang - 29.08.2014
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?
Re: [QUESTION] About OnPlayerCommandText -
Stinged - 29.08.2014
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.
Re: [QUESTION] About OnPlayerCommandText -
vakhtang - 29.08.2014
Thanks dude. I`ll give it a try
Re: [QUESTION] About OnPlayerCommandText -
vakhtang - 29.08.2014
Works!
Thanks