This topic was marked solved by its starter, on July 5th, 04:38 AM

Omniscient

  • New Member
  • Posts: 1
Unable to receive weapon
«  »Last edited
Hello, I am working on a script.I was creating a setwep command.I have created the following command.Whenever i run the command i simply get successful message while i don't receive any weapon.
Here is the code.

Code: [Select]
  else if ( cmd == "setwep" )
     {
            if( !status[player.ID].LoggedIn ) MessagePlayer(" "+red+"You need to login first.", player);
       else  if ( status[player.ID].Level < 2 ) MessagePlayer(" "+dark+"You can not use this command.", player );
      else if ( !text ) MessagePlayer(""+red+" [ERROR] "+yellow+" /" + cmd + " <player> <wep>", player );
  else{
    local params = split(text, " " );
        local plr = FindPlayer(params[0]);
        local wep = GetWeaponID( params[1] );
        local ammo = params[2];
    plr.SetWeapon( wep, ammo );
Message(""+acmd+"Administrator "+player.Name+" has given "+plr.Name+" wep "+wep+" ammo: "+ammo+" ");
  }
     }


Also in another similar command facing  error.

Code: [Select]
   else if ( cmd == "buywep" )
   {
   if ( !text ) MessagePlayer(""+red+"Error: "+white+"Please use /buywep <wepname / id > <ammo>.",player);
       else
   {
      local params = split(text, " ")
      local wep = GetWeaponID( params[0] )
     local ammo = params[1]                                       //Error Here of Index 1
     local q = QuerySQL ( WEP, "SELECT * FROM PremiumWeps WHERE WepID='"+ wep +"' " );   // it also shows here wrong number of parameters
  local cost = GetSQLColumnData( q, 2 ); 
if ( !params[0] )  MessagePlayer(""+red+"Error: "+white+"Please use /buywep <wepname / id > <ammo>.",player);
        else if ( !params[1] ) MessagePlayer(""+red+"Error: "+white+"Please use /buywep <wepname / id > <ammo>.",player);
      else  if ( status[player.ID].Cash < cost ) MessagePlayer(""+red+"Error:"+white+"You don't have enough cash to buy this weapon. Cost: "+cost+"",player);
  else if ( !GetSQLColumnData( q, 0 ) ) MessagePlayer(""+red+"Error:"+white+"Invalid Weapon ID or Name.",player);
  else{

  player.SetWeapon(wep, ammo);
  player.Cash -= cost;
  }
   }
   }

This command is showing the error " index 1 does not exist.I don't know why this is showing error in local params while i used similar method during the insert of weapons price in database and is working well.
Another question is that what can be used for detecting missing arguments.
i tried both but is not working.
Code: [Select]
if ( !params[0] ) MessagePlayer(".......
or
Code: [Select]
if ( params < 1 ) MessagePlayer("........

dracc

  • Jr. Member
  • Posts: 84
Re: Unable to receive weapon
« Reply #1,  »
params.len() can be used to find out the number of elements in your array.

MatheuS

  • Wiki Contributor
  • LBR | Co-Leader & Brasil Real RPG | Developer
  • Posts: 444
Re: Unable to receive weapon
« Reply #2,  »
Code: [Select]
local ammo = params[1]                                       //Error Here of Index 1

if you don't query if the parameter exists, it will give error because it is something incomplete. I'll leave some suggestion below

Code: [Select]
  else if ( cmd == "setwep" )
     {
            if( !status[player.ID].LoggedIn ) MessagePlayer(" "+red+"You need to login first.", player);
            else  if ( status[player.ID].Level < 2 ) MessagePlayer(" "+dark+"You can not use this command.", player );
            else if ( !text ) MessagePlayer(""+red+" [ERROR] "+yellow+" /" + cmd + " <player> <wep>", player );
            else {
                   local params = split(text, " " ), plr = FindPlayer(params[0]), wep = GetWeaponID( params[1] ), ammo = params[2];
                   if( ( !ammo ) || ( !plr  ) || ( !wep ) ) MessagePlayer(""+red+" [ERROR] "+yellow+" /" + cmd + " <player> <wep> <ammo>", player );
                   else {
                                  plr.SetWeapon( wep, ammo );
                                  Message(""+acmd+"Administrator "+player.Name+" has given "+plr.Name+" wep "+wep+" ammo: "+ammo+" ");
                  }
         }
}

not tested xd
Code: [Select]
if( !sucess ) tryAgain();

[SK]Kelvin

  • Jr. Member
  • Posts: 79
Re: Unable to receive weapon
« Reply #3,  »
Code: [Select]
if ( cmd == "setwep" )
{
 if ( !status[ player.ID ].LoggedIn ) MessagePlayer( "" + red + "You need to login first.", player );
 else  if ( status[ player.ID ].Level < 2 ) MessagePlayer( "" + dark + "You can not use this command.", player );
 else if ( !text ) MessagePlayer( "" + red + "[ERROR] " + yellow + " /" + cmd + " <player> <wep>", player );
 else
 {
  local params = split( text, " " );
  if ( params.len() <= 3 )
  {
   local plr = FindPlayer( IsNum( params[ 0 ] ) ? params[ 0 ].tointeger() : params[ 0 ] ), wep = IsNum( params[ 1 ] ) ? params[ 1 ] : GetWeaponID( params[ 1 ] ), ammo = params[ 2 ];
   if ( ( plr ) && ( wep ) && ( ammo ) && ( IsNum( ammo ) ) )
   {
    plr.SetWeapon( wep.tointeger(), ammo.tointeger() );
    Message( "" + acmd + "Administrator " + player.Name + " has given " + plr.Name + " wep " + wep + " ammo: " + ammo + "" );
   }
  }
  else MessagePlayer( "" + red + "[ERROR] " + yellow + " /" + cmd + " <player> <wep>", player );
 }
}

I haven't tested the code, so if it's still not working you can let me know

AroliS^

  • Newbie
  • Learning of Mike Tyson K.O
  • Posts: 46
Re: Unable to receive weapon
« Reply #4,  »Last edited
Quote from [SK]Kelvin on July 4th, 01:37 AM
Code: [Select]
if ( cmd == "setwep" )
{
 if ( !status[ player.ID ].LoggedIn ) MessagePlayer( "" + red + "You need to login first.", player );
 else  if ( status[ player.ID ].Level < 2 ) MessagePlayer( "" + dark + "You can not use this command.", player );
 else if ( !text ) MessagePlayer( "" + red + "[ERROR] " + yellow + " /" + cmd + " <player> <wep>", player );
 else
 {
  local params = split( text, " " );
  if ( params.len() <= 3 )
  {
   local plr = FindPlayer( IsNum( params[ 0 ] ) ? params[ 0 ].tointeger() : params[ 0 ] ), wep = IsNum( params[ 1 ] ) ? params[ 1 ] : GetWeaponID( params[ 1 ] ), ammo = params[ 2 ];
   if ( ( plr ) && ( wep ) && ( ammo ) && ( IsNum( ammo ) ) )
   {
    plr.SetWeapon( wep.tointeger(), ammo.tointeger() );
    Message( "" + acmd + "Administrator " + player.Name + " has given " + plr.Name + " wep " + wep + " ammo: " + ammo + "" );
   }
  }
  else MessagePlayer( "" + red + "[ERROR] " + yellow + " /" + cmd + " <player> <wep>", player );
 }
}

I haven't tested the code, so if it's still not working you can let me know
got error, what happen if the local params = ( text, " " ), could not find anything?   
Code: [Select]
local params; text != null ? params = ( text, " " ) : params = ( "", " " );

 if ( params.len() <= 3 )  above here is a better way
Code: [Select]
if ( params.len() < 2 || params.len() > 2 )

this line is useless
Code: [Select]
else if ( !text ) MessagePlayer( "" + red + "[ERROR] " + yellow + " /" + cmd + " <player> <wep>", player );