Unable to receive weapon

Started by Omniscient, Jul 03, 2019, 05:47 AM

Previous topic - Next topic

Omniscient

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.

  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.

   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.
if ( !params[0] ) MessagePlayer(".......
or
if ( params < 1 ) MessagePlayer("........

dracc

params.len() can be used to find out the number of elements in your array.

MatheuS

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

  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
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

Kelvin Garcia Mendoza

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^

#4
Quote from: [SK]Kelvin on Jul 04, 2019, 12:37 AMif ( 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?   
local params; text != null ? params = ( text, " " ) : params = ( "", " " );
 if ( params.len() <= 3 )  above here is a better way
if ( params.len() < 2 || params.len() > 2 )
this line is useless
else if ( !text ) MessagePlayer( "" + red + "[ERROR] " + yellow + " /" + cmd + " <player> <wep>", player );
Lemme love ya