I have this problem with /ban and reason -
bijoyekuza - 27.02.2011
Hey guys.
I tried to make a /ban command that SendClientMessageToAll's : player %s banned by admin %s reason %s
So this is the code
PHP код:
if(strcmp(cmd,"/ban",true) == 0)
{
if(PlayerInfo[playerid][pAdminLevel] > 1)
{
new tmp[256],tmp2[256],adminname[MAX_PLAYER_NAME],victim,victimname[MAX_PLAYER_NAME],string[256],string2[256];
tmp = strtok(cmdtext, idx);
tmp2 = strtok(cmdtext, idx);
if(strlen(tmp) == 0) return SendClientMessage(playerid,COLOR_RED," : Usage : /ban [PLAYERID][REASON]");
if(strlen(tmp2) == 0) return SendClientMessage(playerid,COLOR_RED,": Usage : /ban [PLAYERID][REASON]");
victim = strval(tmp);
GetPlayerName(victim,victimname,sizeof(victimname));
GetPlayerName(playerid,adminname,sizeof(adminname));
format(string,sizeof(string)," : You have banned : %s",victimname);
new reason[256];
reason = strval(tmp2);
format(string2,sizeof(string2),"Player %s Has been banned by admin : MR.%s . Reason : %s",victimname,adminname,reason);
if(IsPlayerConnected(victim))
{
SendClientMessageToAll(COLOR_RED,string2);
Ban(strval(tmp));
}
if(!IsPlayerConnected(playerid)) return SendClientMessage(playerid,COLOR_RED," This player is not logged in");
}
if(PlayerInfo[playerid][pAdminLevel] < 2)
{
SendClientMessage(playerid,COLOR_AFOR,"You are not authorized to use this command");
}
}
So everything works exept the reason
The error :
(1372) : error 033: array must be indexed (variable "reason")
LINE 1372 is reason = strval(tmp2);
Re: I have this problem with /ban and reason -
pawn_ - 27.02.2011
There's lots of problems in that code, I can't spend time to tell them all to you, sorry.
Here's the fixed code:
pawn Код:
if(strcmp(cmd, "/ban", true) == 0)
{
if(PlayerInfo[playerid][pAdminLevel] > 1)
{
new tmp[128], adminname[MAX_PLAYER_NAME], victim, victimname[MAX_PLAYER_NAME],
string[192], string2[192]; // Why 256 cells?
tmp = strtok(cmdtext, idx); // You defined strtok twice.
if(strlen(tmp) == 0) return SendClientMessage(playerid,COLOR_RED," : Usage : /ban [PLAYERID][REASON]");
victim = strval(tmp);
// this is NOT a coding mistake, as you didn't enter a playerid.
if(strlen(tmp) == 0) return SendClientMessage(playerid,COLOR_RED," : Usage : /ban [PLAYERID][REASON]");
if(victim != INVALID_PLAYER_ID)
{
// for the reason
new length = strlen(cmdtext);
while ((idx < length) && (cmdtext[idx] <= ' '))
{
idx++;
}
new offset = idx;
new result[92]; // Reason is 92 characters long.
while ((idx < length) && ((idx - offset) < (sizeof(result) - 1)))
{
result[idx - offset] = cmdtext[idx];
idx++;
}
result[idx - offset] = EOS;
if(!strlen(result))
{
SendClientMessage(playerid,COLOR_RED," : Usage : /ban [PLAYERID][REASON]");
return 1;
}
GetPlayerName(victim,victimname,sizeof(victimname));
GetPlayerName(playerid,adminname,sizeof(adminname));
format(string,sizeof(string)," : You have banned : %s",victimname);
format(string2,sizeof(string2),"Player %s Has been banned by admin : MR.%s . Reason : %s",victimname,adminname,(result));
// Where's SendClientMessage and Ban?
SendClientMessage(playerid, 0xE21F1FFF, string); // red
SendClientMessageToAll(0xE21F1FFF, string2); // red
BanEx(victim, (result));
return 1;
}
else
{
SendClientMessage(playerid, 0xAFAFAFAA, " Player is disconnected.");
return 1;
}
}
else
{
SendClientMessage(playerid, 0xAFAFAFAA, " You are not an Admin.");
}
// forgot return 1;
return 1;
}
Re: I have this problem with /ban and reason -
bijoyekuza - 28.02.2011
No way that a simple ban command with reason will take so many lines in my script .
I remember One time I made ban command with reason by using strtok and it worked.
I can use scanff but I dont want to use it in my script.
And why now 256 cells ? (at the strings)
Edit :
Dude thanks alot but I got a better command in an easier way and it tottaly works
PHP код:
if(strcmp(cmd,"/ban",true) == 0)
{
if(PlayerInfo[playerid][pAdminLevel] > 1)
{
new tmp[256],tmp2[256],adminname[MAX_PLAYER_NAME],victim,victimname[MAX_PLAYER_NAME],string[256],string2[256];
tmp = strtok(cmdtext, idx);
tmp2 = strtok(cmdtext, idx);
if(strlen(tmp) == 0) return SendClientMessage(playerid,COLOR_RED," : Usage : /ban [PLAYERID][REASON]");
if(strlen(tmp2) == 0) return SendClientMessage(playerid,COLOR_RED,": Usage : /ban [PLAYERID][REASON]");
victim = strval(tmp);
GetPlayerName(victim,victimname,sizeof(victimname));
GetPlayerName(playerid,adminname,sizeof(adminname));
format(string,sizeof(string)," : You have banned : %s",victimname);
new reason[256];
reason = tmp2;
format(string2,sizeof(string2),"Player %s Has been banned by admin : MR.%s . Reason : %s",victimname,adminname,reason);
if(victim != INVAILD_PLAYER_ID {
SendClientMessageToAll(COLOR_RED,string2);
Ban(strval(tmp));
}
else
{
SendClientMessage(playerid,COLOR_RED," This player is not logged in");
}
}
if(PlayerInfo[playerid][pAdminLevel] < 2)
{
SendClientMessage(playerid,COLOR_AFOR,"You are not authorized to use this command");
}
}