timer.Simple issue

So if i Right click then die I get an error

Error:



[ERROR] addons/zombie_swep/lua/weapons/swep_zomb.lua:170: Tried to use a NULL entity!
  1. __index - [C]:-1
   2. unknown - addons/zombie_swep/lua/weapons/swep_zomb.lua:170

Timer Failed! [Simple][@addons/zombie_swep/lua/weapons/swep_zomb.lua (line 169)]


It does this anytime a timer is running and I die so how could I avoid using timers?

You need to check that the entity

IsValid.

How will that stop the timer for giving me errors if I die during it?

It wont pass the if, if the player isn’t a valid entity.



[ERROR] addons/zombie_swep/lua/weapons/swep_zomb.lua:99: attempt to call method 'ShootBullet' (a nil value)
  1. unknown - addons/zombie_swep/lua/weapons/swep_zomb.lua:99

Timer Failed! [Simple][@addons/zombie_swep/lua/weapons/swep_zomb.lua (line 98)]


We need the code to be able to help you.



if not IsValid(trc.Entity) or (self.Owner:EyePos():Distance(trc.Entity:GetPos()) > 100) and self.Owner:IsValid() then
		    timer.Simple(1, function()
			 self.Owner:EmitSound( "npc/zombie/claw_miss1.wav", 80, 100 )
			end )
		else
		  	timer.Simple(1, function()
			 self:ShootBullet( 0, 1, 0 )
			 trc.Entity:TakeDamage( 35, self:GetOwner(), self )
			 self.Owner:EmitSound( HitSounds[math.random(3)], 100, 100 )
			end )
		end


Now what about the second one?

[editline]28th July 2017[/editline]

The IsValid should go INSIDE of the timer.

How so?

[editline]28th July 2017[/editline]

[editline]28th July 2017[/editline]

What do you mean by inside of the timer

Why are you even using a timer.

To have my attack at the same time as the animations

[editline]28th July 2017[/editline]

[editline]28th July 2017[/editline]

I still don’t understand

[editline]28th July 2017[/editline]

Can someone help me?

[editline]28th July 2017[/editline]

:saddowns:

Inside of the function of the timer. So:



-- BLAH
		    timer.Simple(1, function()
                         -- DO THE CHECK RIGHT HERE. (looks gross but you get the point)
                         if IsValid( self.Owner ) then
			 self.Owner:EmitSound( "npc/zombie/claw_miss1.wav", 80, 100 )
                         end
			end )
-- BLAH