Active Weapon?

Hello FP.

i want it to do so if you are holding a ak47 in your hand and type holster in console it’s remove the ak47 and add it to my inventory addon, but can’t get the if to work.

Code:



function TypeHolster( activator, caller )


if Player:GetActiveWeapon() == "weapon_mad_ak47" then

	// Add the item to the inventory
	activator:GiveItem("weapon_mad_ak47", 1);
	// Play Sound
	activator:EmitSound("items/ammocrate_open.wav");
	// Remove Weapon From Player
	activator:StripWeapon("weapon_mad_ak47")
	
end


end

concommand.Add("holster", TypeHolster)


Error:



[lua\autorun\ia_holster.lua:4] attempt to index global 'Player' (a function value)


You can do this 1 of 2 ways:
[lua]
function TypeHolster( Player, caller )

if Player:GetActiveWeapon():GetClass() == “weapon_mad_ak47” then --I think its the class of the gun you want

// Add the item to the inventory
Player:GiveItem("weapon_mad_ak47", 1);
// Play Sound
Player:EmitSound("items/ammocrate_open.wav");
// Remove Weapon From Player
Player:StripWeapon("weapon_mad_ak47")

end

end

concommand.Add(“holster”, TypeHolster)
[/lua]

or

This ones easier and more understanding imo
[lua]
function TypeHolster( activator, caller )

if activator:GetActiveWeapon():GetClass() == “weapon_mad_ak47” then – same here with the class

// Add the item to the inventory
activator:GiveItem("weapon_mad_ak47", 1);
// Play Sound
activator:EmitSound("items/ammocrate_open.wav");
// Remove Weapon From Player
activator:StripWeapon("weapon_mad_ak47")

end

end

concommand.Add(“holster”, TypeHolster)
[/lua]

Thanks Working :slight_smile:

No problem :slight_smile: Do you understand what you were missing though?

yes and got it working with tabels too…