[QUESTION] List of Teams That Can't Attack Mayor

[lua]function playershouldtakedamage(victim, attacker)
if victim:Team() == TEAM_MAYOR && attack:Team() == (sadasdasdasd)
end
hook.Add( “PlayerShouldTakeDamage”, “playershouldtakedamage”, playershouldtakedamage)
[/lua]

How can I make the function check if they attack is one of these teams:


TEAM_POLICE
TEAM_CHIEF
TEAM_SWAT
TEAM_swat_leader
TEAM_swat_sniper
TEAM_VICE

Instead of making a bunch of if/then fuctions… how can I make it all out in 1 function?

**[Table.HasValue

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Table.HasValue)**
[lua]
NoAttackie={TEAM_POLICE,TEAM_CHIEF,TEAM_SWAT,TEAM_swat_leader,TEAM_swat_sniper,TEAM_VICE}

function playershouldtakedamage(victim, attacker)
if victim:Team() == TEAM_MAYOR && table.HasValue(NoAttackie,attacker:Team()) then return false end
end
hook.Add( “PlayerShouldTakeDamage”, “playershouldtakedamage”, playershouldtakedamage)
[/lua]

I think that would work, typed in browser.
Is this what you’re looking for?

Works perfect! Thank you!

New:

It works the first time, but afterwards the mayor starts taking damage again when a cop attacks him.

__gc is getting called on the global table so make it local

[lua]NoAttackie={TEAM_POLICE,TEAM_CHIEF,TEAM_SWAT,TEAM_swat_leader,TEAM_swat_sniper,TEAM_VICE}[/lua]

to

[lua]
local NoAttackie={TEAM_POLICE,TEAM_CHIEF,TEAM_SWAT,TEAM_swat_leader,TEAM_swat_sniper,TEAM_VICE}[/lua]

Would this fix the reason that it stops working randomly

Yes and if you did test it you would’ve saw for yourself and I wouldn’t of even had to reply.

Why would a global get garbage collected? I am very sure it is the other way around. Only locals get garbage collected.

Why does he need it global anyway?