[Lua] Checking if the player has a specific weapon/ammo type on their current weapon and exiting a loop

What I’m trying to do is make a SWep I have that deploys an entity that supplies ammo not give ammo for itself. I think I just have the syntax wrong on the code, but I can’t figure it out. Maybe someone else can see what’s wrong here.

[lua]
function ENT:Think()
if CurTime() >= AmmoDeployDelay then
for _K, _V in pairs(ents.FindInSphere(self:GetPos(), FindRadius)) do
if _V:IsPlayer() and _V:Alive() then
if AmmoCharge > 0 then
_W = _V:GetActiveWeapon()
_PRIAMMO = _W:GetPrimaryAmmoType()
_SECAMMO = _W:GetSecondaryAmmoType()
if _W == (“eq_bag_ammo_bf2” or “eq_bag_medic_bf2” or “eq_c4”) then return false end
if _PRIAMMO == (“ammobag” or “c4” or “medbag”) then return false end
– ammo giving stuff
else
SafeRemoveEntity(self)
end
end

	end
	AmmoDeployDelay = CurTime() + AmmoGiveDelay
end

end
[/lua]

Right now it’s giving ammo for itself in a recursive manner and that’s not what needs to happen.

Show what ‘ammo giving stuff’ is


if	_W == ("eq_bag_ammo_bf2" or "eq_bag_medic_bf2" or "eq_c4") then return false end
					if	_PRIAMMO == ("ammobag" or "c4" or "medbag") then return false end

_W is the weapon entity, and by comparing it with a string, it will never return false. What you probably want to say is this:



if _W:GetClass() ==  ("eq_bag_ammo_bf2" or "eq_bag_medic_bf2" or "eq_c4") then return false end


There’s a similar issue with SWEP:GetPrimaryAmmoType(). When you call that function, it returns the ammo ID of the weapon, which is one of 128 other distinct IDs. You’d have to do something like this for that line:



if	_PRIAMMO == (game.GetAmmoID("ammobag") or game.GetAmmoID("c4") or game.GetAmmoID("medbag")) then return false end


What this does is take the string name of an ammo and return its numerical ID, which is what _PRIAMMO is defined as.

Worked like a charm! Thanks for that!