Help with Weapon buy by command
#1

English Version:
I got this script to buy weapons with the command / comprararmas [id]. When I type the command to buy the grenade, the purchase is completed successfully. But to buy other weapons, just get the grenade, even without compilation errors. How to set respectively the weapon at his command? Sorry for bad English, is that I'm Brazilian.
Brazilian Version
Eu tenho esse script para comprar armas atravйs do comando /comprararmas [id]. Quando eu digito o comando para comprar a granada, a compra й feita com sucesso. Mas para comprar outras armas, apenas recebo a granada, mesmo sem erros de compilaзгo. Como fazer para receber a arma respectivamente ao seu comando?
Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
	if (strcmp("/armas", cmdtext, true, 10) == 0)
	{
		SendClientMessage(playerid,0x00F6F6AA,"Para comprar as suas armas /comprararma [id]");
		SendClientMessage(playerid,0x00F6F6AA,"Nem todas as armas estгo listadas para ninguйm ter vantagem sobre ninguйm");
		SendClientMessage(playerid,0x00F6F6AA,"Granada---16");
		SendClientMessage(playerid,0x00F6F6AA,"Pistola 9mm---22");
		SendClientMessage(playerid,0x00F6F6AA,"Desert Eagle---24");
		SendClientMessage(playerid,0x00F6F6AA,"Sawn-off Shotgun---26");
		SendClientMessage(playerid,0x00F6F6AA,"Combat Shotgun---27");
		SendClientMessage(playerid,0x00F6F6AA,"Micro SMG---28");
		SendClientMessage(playerid,0x00F6F6AA,"AK47---29");
		SendClientMessage(playerid,0x00F6F6AA,"M4---31");
		return 1;
}
	if (strcmp("/comprararma 16", cmdtext, true, 10) == 0)
	{
	GivePlayerWeapon(playerid,16,350);
	GivePlayerMoney(playerid,-500);
	SendClientMessage(playerid,COLOR_GREEN,"Arma comprada com sucesso!");
	return 1;
}
	if (strcmp("/comprararma 22", cmdtext, true, 10) == 0)
	{
	GivePlayerWeapon(playerid,22, 350);
	GivePlayerMoney(playerid,-500);
	SendClientMessage(playerid,COLOR_GREEN,"Arma comprada com sucesso!");
	return 1;
}
  	if (strcmp("/comprararma 24", cmdtext, true, 10) == 0)
  	{
  	GivePlayerWeapon(playerid,24,350);
  	GivePlayerMoney(playerid,-500);
  	SendClientMessage(playerid,COLOR_GREEN,"Arma comprada com sucesso!");
  	return 1;
}
  if (strcmp("/comprararma 26", cmdtext, true, 10) == 0)
  {
  GivePlayerWeapon(playerid,26,350);
  GivePlayerMoney(playerid,-500);
  SendClientMessage(playerid,COLOR_GREEN,"Arma comprada com sucesso!");
  return 1;
}
  if (strcmp("/comprararma 27", cmdtext, true, 10) == 0)
  {
  GivePlayerWeapon(playerid,27,350);
  GivePlayerMoney(playerid,-500);
  SendClientMessage(playerid,COLOR_GREEN,"Arma comprada com sucesso!");
  return 1;
}
  if (strcmp("/comprararma 28", cmdtext, true, 10) == 0)
  {
  GivePlayerWeapon(playerid,28,350);
  GivePlayerMoney(playerid,-500);
  SendClientMessage(playerid,COLOR_GREEN,"Arma comprada com sucesso!");
  return 1;
}
  if (strcmp("/comprararma 29", cmdtext, true, 10) == 0)
  {
  GivePlayerWeapon(playerid,29,350);
  GivePlayerMoney(playerid,-500);
  SendClientMessage(playerid,COLOR_GREEN,"Arma comprada com sucesso!");
  return 1;
}
  if (strcmp("/comprararma 31", cmdtext, true, 10) == 0)
  {
  GivePlayerWeapon(playerid,31,350);
  GivePlayerMoney(playerid,-500);
  SendClientMessage(playerid,COLOR_GREEN,"Arma comprada com sucesso!");
  return 1;
}
	return 0;
}
Reply
#2

Its because you use the length variable 10.
(const string1[], const string2[], bool:ignorecase, length)
^
So when you do strcmp("/comprararma 16", cmdtext, true, 10)
you are only comparing the first 10 charachters:
"/comprarar" so all of your /comprararma # commands match.
You can either change the length to 15 ("/comprararma 16")
or just remove it entirely.


You are also wasting a lot of time doing strcmp for each of the possible entries.

Here is a much more efficient version of your code that should work
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
    new cmd[128], idx;
    cmd = strtok(cmdtext, idx);

    if (strcmp("/armas", cmd, true) == 0)
    {
        SendClientMessage(playerid,0x00F6F6AA,"Para comprar as suas armas /comprararma [id]");
        SendClientMessage(playerid,0x00F6F6AA,"Nem todas as armas estгo listadas para ninguйm ter vantagem sobre ninguйm");
        SendClientMessage(playerid,0x00F6F6AA,"Granada---16");
        SendClientMessage(playerid,0x00F6F6AA,"Pistola 9mm---22");
        SendClientMessage(playerid,0x00F6F6AA,"Desert Eagle---24");
        SendClientMessage(playerid,0x00F6F6AA,"Sawn-off Shotgun---26");
        SendClientMessage(playerid,0x00F6F6AA,"Combat Shotgun---27");
        SendClientMessage(playerid,0x00F6F6AA,"Micro SMG---28");
        SendClientMessage(playerid,0x00F6F6AA,"AK47---29");
        SendClientMessage(playerid,0x00F6F6AA,"M4---31");
        return 1;
    }
    if (strcmp("/comprararma", cmd, true) == 0)
    {
    new tmp[28], num;
    tmp = strtok(cmdtext, idx);
    if(!strlen(tmp)) return 0;
    num = strval(tmp);
    switch(num)
        {
            case 16, 22, 24, 26, 27, 28, 29, 31:
            {
                GivePlayerWeapon(playerid,num,350);
                GivePlayerMoney(playerid,-500);
                SendClientMessage(playerid,COLOR_GREEN,"Arma comprada com sucesso!");
            }
        }
        return 1;
    }
    return 0;
}
I would personally suggest going even further and using dcmd
Reply
#3

Thanks man! I am very grateful. Just one thing. When I type the command for example /commandtest I put the number of letters of the command after the "true"?
Reply
#4

/commandtest? thats not a part of the script? Not sure what you mean.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)