ZCMD Command is executed twice
#1

Hello,

I have created a command to create items but when it's typed ingame, it executes the command twice.
It will send twice the message and give you 2 items.
What is the problem? what did i do wrong?

Код:
CMD:createitem(playerid,params[])
{
	new modelid, str[128];
	if(AdminLevel[playerid] >=5)
    {
    if(PlayerInfo[playerid][aDuty] == 0) return SendClientMessage(playerid,GREY,"You are not on admin duty.");
    if(sscanf(params,"i",modelid)) return SendClientMessage(playerid,GREY,"USAGE: /createitem [itemid]");
    SaveInventory(playerid);
    if(modelid == 19942)
   	{
	   	AddItem(playerid, modelid);
	   	Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvRadio] += 1;
      	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
	}
	if(modelid == 19141)
   	{
	   	AddItem(playerid, modelid);
	   	Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvPoliceHelmet] += 1;
      	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
	}
	if(modelid == 19110)
   	{
	   	AddItem(playerid, modelid);
	   	Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvArmyHelmet] += 1;
      	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
	}
    if(modelid == 19567)
   	{
	   	AddItem(playerid, modelid);
	   	Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvGunpowder] += 1;
      	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
	}
	if(modelid == 19568)
   	{
	   	AddItem(playerid, modelid);
	   	Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvLead] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
	}
    if(modelid == 341)
   	{
	   	AddItem(playerid, modelid);
	   	Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvChainsaw] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
	}
   	if(modelid == 3026)
   	{
	   	AddItem(playerid, modelid);
	   	Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvBag] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
	}
    if(modelid == 346)
   	{
		AddItem(playerid, modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][Inv9mm] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
   	}
    if(modelid == 336)
  	{
	   	AddItem(playerid, modelid);
	   	Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvBat] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
	}
   	if(modelid == 849)
    {
    	AddItem(playerid ,modelid);
     	Inventory[playerid][InvSlots] += 1;
      	Inventory[playerid][InvInduScraps] += 1;
       	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
    }
  	if(modelid == 2902)
 	{
 		AddItem(playerid,modelid);
 		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvPlasticScraps] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 18016)
 	{
 		AddItem(playerid,modelid);
 		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvMetalScraps] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 1463)
 	{
 		AddItem(playerid,modelid);
 		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvLogs] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 331)
 	{
 		AddItem(playerid,modelid);
 		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvBrass] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 347)
 	{
 		AddItem(playerid,modelid);
        Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvS9mm] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 348)
 	{
 		AddItem(playerid,modelid);
 		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvDeagle] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 349)
 	{
 		AddItem(playerid,modelid);
 		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvShotgun] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 350)
 	{
 		AddItem(playerid,modelid);
 		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvSawedoff] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
	if(modelid == 351)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvSpas12] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 352)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvUzi] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 353)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvMP5] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 355)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvAK47] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 356)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvM4] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 372)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvTEC9] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 357)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvRifle] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 358)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvSniper] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 19110)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvArmyHelmet] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 19141)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvArmyHelmet] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 371)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvParachute] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 335)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvKnife] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 337)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvShovel] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 367)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvCamera] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 325)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvFlowers] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 19515)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvKevlar] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 19036)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvMask] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 19577)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvApple] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 19578)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvBanana] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 19574)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvOrange] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 19570)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvWater] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 19921)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvToolkit] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 11738)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvMedkit] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 11736)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvBandages] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
  	if(modelid == 3044)
 	{
		AddItem(playerid,modelid);
		Inventory[playerid][InvSlots] += 1;
   		Inventory[playerid][InvCigar] += 1;
     	format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
     	SendClientMessage(playerid, GREEN , str);
  	}
	}else return SendClientMessage(playerid,GREY,"You are not an admin");
    return 1;
}
Reply
#2

Try to change all if with else if, or using switch
Reply
#3

use switch
Reply
#4

Could someone provide me with an example ?
Reply
#5

PHP код:
switch (classid)
    {
        case 
01//change
        
{
            
//code
        
}
                case 
23// change
                
{
                      
//code
                
}
    } 
Reply
#6

I re-wrote your code using switch statement, also improving the efficiency:

Код:
CMD:createitem(playerid,params[])
{
	  new modelid, str[128];
    if(AdminLevel[playerid] >=5)
    {
      if(PlayerInfo[playerid][aDuty] == 0) return SendClientMessage(playerid,GREY,"You are not on admin duty.");
      if(sscanf(params,"i",modelid)) return SendClientMessage(playerid,GREY,"USAGE: /createitem [itemid]");
      SaveInventory(playerid);

      AddItem(playerid, modelid);
      Inventory[playerid][InvSlots] += 1;
      switch(modelid)
      {
          case 19942: Inventory[playerid][InvRadio] += 1;
          case 19141: Inventory[playerid][InvPoliceHelmet] += 1;
          case 19110: Inventory[playerid][InvArmyHelmet] += 1;
          case 19567: Inventory[playerid][InvGunpowder] += 1;
          case 19568: Inventory[playerid][InvLead] += 1;
          case 341: Inventory[playerid][InvChainsaw] += 1;
          case 3026: Inventory[playerid][InvBag] += 1;
          case 346: Inventory[playerid][Inv9mm] += 1;
          case 336: Inventory[playerid][InvBat] += 1;
          case 849: Inventory[playerid][InvInduScraps] += 1;
          case 2902: Inventory[playerid][InvPlasticScraps] += 1;
          case 18016: Inventory[playerid][InvMetalScraps] += 1;
          case 1463: Inventory[playerid][InvLogs] += 1;
          case 325: Inventory[playerid][InvFlowers] += 1;
          case 331: Inventory[playerid][InvBrass] += 1;
          case 335: Inventory[playerid][InvKnife] += 1;
          case 337: Inventory[playerid][InvShovel] += 1;
          case 347: Inventory[playerid][InvS9mm] += 1;
          case 348: Inventory[playerid][InvDeagle] += 1;
          case 349: Inventory[playerid][InvShotgun] += 1;
          case 350: Inventory[playerid][InvSawedoff] += 1;
          case 351: Inventory[playerid][InvSpas12] += 1;
          case 352: Inventory[playerid][InvUzi] += 1;
          case 353: Inventory[playerid][InvMP5] += 1;
          case 355: Inventory[playerid][InvAK47] += 1;
          case 356: Inventory[playerid][InvM4] += 1;
          case 357: Inventory[playerid][InvRifle] += 1;
          case 358: Inventory[playerid][InvSniper] += 1;
          case 367: Inventory[playerid][InvCamera] += 1;
          case 371: Inventory[playerid][InvParachute] += 1;
          case 372: Inventory[playerid][InvTEC9] += 1;
          case 19515: Inventory[playerid][InvKevlar] += 1;
          case 19306: Inventory[playerid][InvMask] += 1;
          case 19577: Inventory[playerid][InvApple] += 1;
          case 19578: Inventory[playerid][InvBanana] += 1;
          case 19574: Inventory[playerid][InvOrange] += 1;
          case 19570: Inventory[playerid][InvWater] += 1;
          case 19921: Inventory[playerid][InvToolkit] += 1;
          case 11738: Inventory[playerid][InvMedkit] += 1;
          case 11736: Inventory[playerid][InvBandages] += 1;
          case 3044: Inventory[playerid][InvCigar] += 1;
          //case 19110: Inventory[playerid][InvArmyHelmet] += 1; In this case you have the same modelid of another item
          //case 19141: Inventory[playerid][InvArmyHelmet] += 1; In this case you have the same modelid of another item
      }
      format(str,sizeof(str),"Item created: %s",GetItemName(modelid));
      SendClientMessage(playerid, GREEN , str);

	}else return SendClientMessage(playerid,GREY,"You are not an admin");
    return 1;
}
Reply
#7

You can easily write your command as below, instead of your holly molly way:

PHP код:
CMD:createitem(playerid,params[])
{
    new 
modelidstr[128];
    if(
AdminLevel[playerid] < 5) return SendClientMessage(playerid,GREY,"You are not an admin");
    if(
PlayerInfo[playerid][aDuty] == 0) return SendClientMessage(playerid,GREY,"You are not on admin duty.");
    if(
sscanf(params,"i",modelid)) return SendClientMessage(playerid,GREY,"USAGE: /createitem [itemid]");
    
format(str,sizeof(str),"Item created: %s",GetItemName(modelid)); SendClientMessage(playeridGREEN str);
    
SaveInventory(playerid); AddItem(playeridmodelid); Inventory[playerid][InvSlots] += 1;
    switch(
modelid)
    {
        case 
19942Inventory[playerid][InvRadio] += 1;
        case 
19141Inventory[playerid][InvPoliceHelmet] += 1;
        case 
19110Inventory[playerid][InvArmyHelmet] += 1;
        case 
19567Inventory[playerid][InvGunpowder] += 1;
        case 
19568Inventory[playerid][InvLead] += 1;
        
// And so on ...
    
}
    return 
1;

EDIT: @pollo97 just said it.
Reply
#8

Yes forgive me for my messy pile of code as im still pretty new in SAMP.
Thank you for your response. This will obviously help me improving my code.
After all i found out why the message got executed twice.
I have some object multiple times in the script and due it's messy style i did not notice.
Thank you.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)