Making my mines ignore traitors

This is my 1st lua attempt.

I get no errors in console but it blows up on traitors.


function ENT:Think()
        local t = {}
        t.start = self:GetPos()
        t.endpos = t.start + self:GetUp() * 4096
        t.filter = self
        local tr = util.TraceLine(t)
        self.endpos = tr.HitPos
        if tr.HitNonWorld then
    
    if SERVER then

local ply = self.Owner
    local ignore = {ply, self}
     if not IsValid(ply) then return end
    for k, v in pairs(player.GetAll()) do
    if v:GetRole() == ROLE_TRAITOR then
    table.insert(ignore, v)
     end
    if (not table.HasValue(ignore,tr.Entity)) and tr.Entity:IsPlayer() then
    self:Explosive()
    self:Remove()  
end
end
end
end 
end  



function ENT:StartTouch( ent )
	if (ent:IsPlayer()) then
		if (ent:GetRole() != ROLE_TRAITOR) then
			self:Explosive()
			self:Remove()
		end
	end
end

Didn’t work, I must be doing something wrong.
I disabled the


      if tr.HitNonWorld then
    
    if SERVER then

local ply = self.Owner
    local ignore = {ply, self}
     if not IsValid(ply) then return end
    for k, v in pairs(player.GetAll()) do
    if v:GetRole() == ROLE_TRAITOR then
    table.insert(ignore, v)
     end
    if (not table.HasValue(ignore,tr.Entity)) and tr.Entity:IsPlayer() then
    self:Explosive()
    self:Remove()  

and placed the StartTouch in below the Think function. I’m planning on releasing this to the public anyways since I’ve put this together with stuff from pastebin and github.

wep - http://pastebin.com/kVGtqhs0
ent - http://pastebin.com/pmD0UxYz

I know this code is horrible but I have to learn somehow.

You’re overcomplicating things. Keep in mind that Think is called very frequently, and you are basically creating a new table, filling it with players, and then calling table.HasValue on it, all of that about 60 times per second, if not more. That’s a huge waste of memory and resources, especially since table.HasValue is the kind of expensive function that should be avoided at all costs.

[lua]function ENT:Think()
local t = {}
t.start = self:GetPos()
t.endpos = t.start + self:GetUp() * 4096
t.filter = self
local tr = util.TraceLine(t)
self.endpos = tr.HitPos

if SERVER then
	if tr.HitNonWorld then
		if tr.Entity:IsPlayer() and tr.Entity:GetRole() == ROLE_TRAITOR then
			self:Explosive()
			self:Remove()
		end
	end
end

end[/lua]

This is much simpler (and it should work). It simply takes the entity hit by the trace, check if it’s a player, and if that player is a traitor, and then explodes if those conditions are met. There’s also no need to check if the trace hit itself (it can’t, since you already filtered it out), and there’s no need to check for its owner since it’s very unlikely that its owner is a traitor.

The StartTouch solution doesn’t work because your mine is a tripmine. StartTouch is simply called when something touches the entity itself, not when something crosses the beam.
Initialize, OnTakeDamage and Explosive should be serverside only, since they only do serverside actions. You shouldn’t initialize the physics of an entity serverside and clientside at the same time.

Also you should really learn to indent your code. It makes it a lot more readable. You can do that by using the TAB key, which inserts a large space.

Thank you for the awesome advice and your code works except I needed the exact opposite. I need it not to explode on traitors and explode on innos/det, I can manage that much myself and thank you for showing me the right way to do it.

EDIT: I have a bit of a problem with the beam and the hit detection, if the mine ent isn’t on the screen then the beam wont be visible and if you run through the beam fast then it sometimes will not register.