SA-MP Forums Archive
Checking if a player has a certain weapon in hand. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Checking if a player has a certain weapon in hand. (/showthread.php?tid=618138)



Checking if a player has a certain weapon in hand. - Swarn - 01.10.2016

Basically, I need help with a coding, what it's suppose to do is, if the player possesses a Shotgun, it attaches a Shotgun to their back. I got the Shotgun sticking to their back code, I just need help with the, checking if a player has a gun, and where the code should go. I'm not sure where to place this code, thanks.

Код:
if(GetPlayerWeapon(playerid) != 25)
{
    // attaches the shotgun to players back
}



Re: Checking if a player has a certain weapon in hand. - Battallboi - 01.10.2016

Put this at the end of the gamemode.

Код:
   stock gCheckPlayerWeapon(playerid, _weaponid)
   {
      const MAX_WEAPONS_SLOTS = 13;
      static ammo, weaponid;
      for (new i; i != MAX_WEAPONS_SLOTS; i++)
   {
      if (GetPlayerWeaponData(playerid, i, weaponid, ammo) == 0)
      return 0;
      if (weaponid == _weaponid)
      return 1;
   }
   return 0;
   }
And put this one in the OnPlayerSpawn public.
Код:
if(gCheckPlayerWeapon(playerid, putweaponidhere)) //put the id of the weapon that you desire
{
//attach shotgun code
}



Re: Checking if a player has a certain weapon in hand. - Swarn - 01.10.2016

Quote:
Originally Posted by Battallboi
Посмотреть сообщение
Put this at the end of the gamemode.

Код:
   stock gCheckPlayerWeapon(playerid, _weaponid)
   {
      const MAX_WEAPONS_SLOTS = 13;
      static ammo, weaponid;
      for (new i; i != MAX_WEAPONS_SLOTS; i++)
   {
      if (GetPlayerWeaponData(playerid, i, weaponid, ammo) == 0)
      return 0;
      if (weaponid == _weaponid)
      return 1;
   }
   return 0;
   }
And put this one in the OnPlayerSpawn public.
Код:
if(gCheckPlayerWeapon(playerid, putweaponidhere)) //put the id of the weapon that you desire
{
//attach shotgun code
}
Doesn't work, I spawn the weapon and it doesn't place gun on my back


Re: Checking if a player has a certain weapon in hand. - Ner0x96 - 01.10.2016

Quote:
Originally Posted by Swarn
Посмотреть сообщение
Doesn't work, I spawn the weapon and it doesn't place gun on my back
Maybe you have to put

Код:
if(gCheckPlayerWeapon(playerid, putweaponidhere)) //put the id of the weapon that you desire
{
//attach shotgun code <<<<<<<<<<<<<<<<<<<<<<<<< HERE
}
Your attach code hm (?)


Re: Checking if a player has a certain weapon in hand. - Swarn - 01.10.2016

Yeah, I put the attach code in, if you need it, here;
Код:
SetPlayerAttachedObject(playerid, 4,349,1,-0.102073, -0.200483, 0.094914, 15.186383, 61.980533, 357.054473);



Re: Checking if a player has a certain weapon in hand. - Ner0x96 - 01.10.2016

Okay, then put this:

Код:
if(gCheckPlayerWeapon(playerid, putweaponidhere)) //put the id of the weapon that you desire
{
SetPlayerAttachedObject(playerid, 4,349,1,-0.102073, -0.200483, 0.094914, 15.186383, 61.980533, 357.054473);
}
in OnPlayerUpdate and test it


Re: Checking if a player has a certain weapon in hand. - Swarn - 01.10.2016

Yeah, that works, thanks.
But slight problem

When the player shoots all the ammo out, the weapon stays on his back, how to fix?


Re: Checking if a player has a certain weapon in hand. - Ner0x96 - 01.10.2016

Код:
new wep[2];
GetPlayerWeaponData(playerid, i, wep[0], wep[1]); // wep[0] = weapon ID - wep[1] = ammo of the weapon
if(gCheckPlayerWeapon(playerid, putweaponidhere))
{
	if(wep[0] == IDWEAP && wep[1] == 0) RemovePlayerAttachedObject(playerid, 4);
	SetPlayerAttachedObject(playerid, 4, 349,1,-0.102073, -0.200483, 0.094914, 15.186383, 61.980533, 357.054473); // Here put the attach of the weapon that you want
}
Try with this


Re: Checking if a player has a certain weapon in hand. - Swarn - 01.10.2016

Where do I put that?


Re: Checking if a player has a certain weapon in hand. - Luis- - 01.10.2016

I wouldn't recommend putting it on OnPlayerUpdate, put it in a timer.