How do I prevent an entity doing damage to owner?

I’m currently working on a SWEP that fires entities, doing 5 damage to everything but not to the player that fired it.



function ENT:PhysicsCollide( data, physobj )

	local hitent = data.HitEntity --Entity that is hit = HitEnt

	if hitent == self.Entity:GetOwner() then
		return 1
		
	elseif hitent:IsPlayer() and hitent != self.Entity:GetOwner() then  --Cannot hit Owner
		hitent:TakeDamage( 5, self.Entity:GetOwner() ) --Damage player
		hitent:ViewPunch( Angle( math.random(-1, 1), math.random(-1,1), math.random(-1,1) ) )  --Throw their aim
		self.Entity:Remove() --Remove shot
	
	elseif hitent:IsWorld() then
		self.Entity:Remove()  --Remove if hits world

	elseif hitent:IsNPC() then
		hitent:TakeDamage( 7, self.Entity:GetOwner() )  --Damages NPCs
		self.Entity:Remove()

	else
		hitent:TakeDamage( 5, self.Entity:GetOwner() ) --Damages props, etc.
		self.Entity:Remove()
	end
end



However, when the entity is frozen with the physgun and comes into contact with the owner it does damage. This damage is obviously as a result of the IsPlayer section as it also causes a view punch.

Any help on this problem would be appreciated. Also, is there any way to prevent the ability to physgun the entity? And on an unrelated note, the entities occasionally bounce off each something when rapidly firing at an NPC. Any suggestions as to what this may be?

Many thanks.

Try this

[lua]
function notpickingup( ply, ent )
if ent:GetClass() == “nameofentity” then
return false
else
return true
end
end

hook.Add( “PhysgunPickup”, “physgunPickup”, notpickingup )

[/lua]

Instead of using an if statement, you could just do this:
[lua]function notpickingup(ply, ent)
return !ent:GetClass() == “nameofentity”
end[/lua]

Thanks, that works brilliantly. Any suggestions for the damage issue?

Shouldn’t it be more like this?
[lua]
function notpickingup(ply, ent)
if ent:GetClass() == “nameofentity” then
return false
end
end
[/lua]
You shouldn’t return anything in hooks like these unless you have to. Otherwise you’ll override all the other PhysgunPickup hooks and they won’t get to apply their own conditions to the event.

Yes I know, I wasn’t talking about the context the code was being used, I was just saying that the way that sintwins was doing it is not the most simple way.

I see. You’re right there, using boolean logic straight in the return line iss much nicer than an if/else.