Make a function occur if an item is *not* on a table?

Hey guys, I’m having a bit of trouble modifying an addon (for private use). Essentially, the original version gives a list/table of weapons that won’t be allowed to be given ammo when the box is activated. I want to reverse this function and make it so that the only weapons that are allowed to be resupplied are the ones on the list, with all others being denied. Here’s the original code. (FuckTheseGuns is the list of guns that can’t be resupplied)


function ENT:Use(activator, caller)
        ammountofrandomammo = math.random(15,35)
        weapon = activator:GetActiveWeapon()
 
        if !IsValid(weapon) then return end
       
        for p, q in pairs(FuckTheseGuns) do
                if weapon:GetClass() == q then
                        activator:PrintMessage(HUD_PRINTTALK, "This weapon may not be resupplied with this pickup." )
                        activator:EmitSound("Player.DenyWeaponSelection")
                return end
        end
       
        for p, q in pairs(donotgranttheseweaponsammo) do
                if weapon:GetClass() == q then
                        activator:PrintMessage(HUD_PRINTTALK, "This weapon can't be resupplied." )
                        activator:EmitSound("Player.DenyWeaponSelection")
                return end
        end
       
                       
        activator:GiveAmmo( ammountofrandomammo ,  weapon:GetPrimaryAmmoType(), false)
        activator:PrintMessage(HUD_PRINTTALK, "You picked up some ammo." )
        activator:EmitSound("AmmoCrate.Open")
       
        self:Remove()
end
end

I would convert the key-value into a hashtable so you could do an easy string comparison:


local FuckTheseGuns = {
   weapon_foo = true,
   weapon_bar = true
}

But if you absolutely need to keep the original table structure, you can change the loop to:


local bNoResupply = true

for p, q in pairs(FuckTheseGuns) do
	if weapon:GetClass() == q then
		bNoResupply = false
		break
	end
end

if bNoResupply then
	activator:PrintMessage(HUD_PRINTTALK, "This weapon may not be resupplied with this pickup." )
	activator:EmitSound("Player.DenyWeaponSelection")
	return
end

You could alternatively use a goto statement.

The second option worked great, thanks a ton :smiley: