SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 01/10/2016, 06:37 PM   #1
Swarn
Big Clucker
 
Join Date: Sep 2015
Posts: 135
Reputation: 19
Default Checking if a player has a certain weapon in hand.

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.

Code:
if(GetPlayerWeapon(playerid) != 25)
{
    // attaches the shotgun to players back
}
Swarn is offline   Reply With Quote
Old 01/10/2016, 07:57 PM   #2
Battallboi
Little Clucker
 
Join Date: Sep 2016
Posts: 8
Reputation: 0
Default Re: Checking if a player has a certain weapon in hand.

Put this at the end of the gamemode.

Code:
   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.
Code:
if(gCheckPlayerWeapon(playerid, putweaponidhere)) //put the id of the weapon that you desire
{
//attach shotgun code
}
Battallboi is offline   Reply With Quote
Old 01/10/2016, 08:22 PM   #3
Swarn
Big Clucker
 
Join Date: Sep 2015
Posts: 135
Reputation: 19
Default Re: Checking if a player has a certain weapon in hand.

Quote:
Originally Posted by Battallboi View Post
Put this at the end of the gamemode.

Code:
   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.
Code:
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
Swarn is offline   Reply With Quote
Old 01/10/2016, 08:35 PM   #4
Ner0x96
Banned
 
Join Date: Oct 2014
Posts: 68
Reputation: 15
Default Re: Checking if a player has a certain weapon in hand.

Quote:
Originally Posted by Swarn View Post
Doesn't work, I spawn the weapon and it doesn't place gun on my back
Maybe you have to put

Code:
if(gCheckPlayerWeapon(playerid, putweaponidhere)) //put the id of the weapon that you desire
{
//attach shotgun code <<<<<<<<<<<<<<<<<<<<<<<<< HERE
}
Your attach code hm (?)
Ner0x96 is offline   Reply With Quote
Old 01/10/2016, 08:56 PM   #5
Swarn
Big Clucker
 
Join Date: Sep 2015
Posts: 135
Reputation: 19
Default Re: Checking if a player has a certain weapon in hand.

Yeah, I put the attach code in, if you need it, here;
Code:
SetPlayerAttachedObject(playerid, 4,349,1,-0.102073, -0.200483, 0.094914, 15.186383, 61.980533, 357.054473);
Swarn is offline   Reply With Quote
Old 01/10/2016, 09:01 PM   #6
Ner0x96
Banned
 
Join Date: Oct 2014
Posts: 68
Reputation: 15
Default Re: Checking if a player has a certain weapon in hand.

Okay, then put this:

Code:
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
Ner0x96 is offline   Reply With Quote
Old 01/10/2016, 09:05 PM   #7
Swarn
Big Clucker
 
Join Date: Sep 2015
Posts: 135
Reputation: 19
Default Re: Checking if a player has a certain weapon in hand.

Yeah, that works, thanks.
But slight problem

When the player shoots all the ammo out, the weapon stays on his back, how to fix?
Swarn is offline   Reply With Quote
Old 01/10/2016, 09:15 PM   #8
Ner0x96
Banned
 
Join Date: Oct 2014
Posts: 68
Reputation: 15
Default Re: Checking if a player has a certain weapon in hand.

Code:
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
Ner0x96 is offline   Reply With Quote
Old 01/10/2016, 09:31 PM   #9
Swarn
Big Clucker
 
Join Date: Sep 2015
Posts: 135
Reputation: 19
Default Re: Checking if a player has a certain weapon in hand.

Where do I put that?
Swarn is offline   Reply With Quote
Old 01/10/2016, 09:33 PM   #10
Luis-
High-roller
 
Luis-'s Avatar
 
Join Date: Jan 2010
Location: England
Posts: 3,933
Reputation: 420
Default Re: Checking if a player has a certain weapon in hand.

I wouldn't recommend putting it on OnPlayerUpdate, put it in a timer.
__________________
Luis- is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking if a player has weapon equipment Josh_Main Scripting Help 2 03/10/2014 02:15 PM
Check if player has a specific weapon in hand. Unri Scripting Help 3 31/10/2013 10:45 AM
Weapon holding [One Hand/Two Hands] Rocketeer Help Archive 2 26/07/2011 05:05 AM
[HELP] How i use 1 weapon in hand Tosermann Help Archive 2 26/03/2010 09:59 AM
Checking if player has weapon, using an array or another method... Robbin237 Help Archive 7 15/02/2009 12:42 PM


All times are GMT. The time now is 07:47 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.