Failing Timer

Hi all, I just need your help with something. I am fixing an old addon from Gmod 12 to work in Gmod 13. I have fixed most of the bugs present and it works fine except for one thing; There is an error with a failing timer that I just cannot fix. This is the error as reoprted in the command console:

[ERROR] lua/weapons/gmod_tool/stools/cannon.lua:119: attempt to compare number with nil

  1. FadeOut - lua/weapons/gmod_tool/stools/cannon.lua:119
  2. unknown - lua/entities/sd_cannonball/init.lua:58

Timer Failed! [Simple][@lua/entities/sd_cannonball/init.lua (line 58)]

Any help would be appreciated as this bug is getting on my nerves as it is the only one I have not been able to fix on my own.

You’re attempting to compare a number with nil.

We can’t help if you don’t post the code.

Ah yes, I knew that I had forgotten something. The code is thus:

Line 58 from init.lua:


function ENT:Destruct( delself, explode, destroy, ignite ) --this is where things get AWESOME (delself kills the ball, explode makes it explode)
	for k,v in pairs( self:GetNearProps(75) ) do --look at the props around the ball
		if v:IsValid() then
			local class = v:GetClass()
			if destroy then
				if v:IsNPC() or class == "prop_physics" or class == "sd_res" or class == "gmod_wheel" or class == "phys_magnet" then --if it's a general prop or npc...
					timer.Simple( 15, function() if v:IsValid() then FadeOut( v ) end end ) - (This is line 58)
					v:GetPhysicsObject():EnableMotion( true ) --unfreeze it!
					if constraint.HasConstraints( v ) then --if it has any constraints...
						constraint.RemoveAll( v ) --undo them!
					end
					if ignite then 
						v:Ignite( 10, 10 ) --ignite it!
						timer.Simple( 9, function() if v:IsValid() then v:SetMaterial( "models/props_debris/plasterwall009d.vmt" ) end end )
					end
				elseif v:IsPlayer() then --if it's a player...
					if explode then --if we want splash damage...
						v:Kill() --kill it!
					end
				end
			end
		end
	end

Line 119 from cannon.lua:


if SERVER then
	function FadeOut( ent )
		if ent:IsValid() then
			ent:SetCollisionGroup( COLLIDE_GROUP_DEBRIS )
			local r,g,b,a = ent:GetColor()
			if a > 150 then - (This is line 119)
				if ent.sd != null then
					ent.sd.grow = false
				end
				newA = a-5
				ent:SetColor( r, g, b, newA )
				timer.Simple( 0.1, FadeOut, ent )
			else
				ent:Remove()
			end
		end
	end

local col = ent:GetColor()
local r, g, b, a = col.r, col.g, col.b, col.a

I have put the code into cannon.lua:


if SERVER then
	function FadeOut( ent )
		if ent:IsValid() then
			ent:SetCollisionGroup( COLLIDE_GROUP_DEBRIS )
			local col = ent:GetColor()
            local r, g, b, a = col.r, col.g, col.b, col.a
			if a > 150 then
				if ent.sd != null then
					ent.sd.grow = false
				end
				newA = a-5
				ent:SetColor( r, g, b, newA )
				timer.Simple( 0.1, FadeOut, ent )
			else
				ent:Remove()
			end
		end
	end

This is now creating a new error:

[ERROR] lua/weapons/gmod_tool/stools/cannon.lua:116: attempt to index local ‘ent’ (a nil value)

  1. unknown - lua/weapons/gmod_tool/stools/cannon.lua:116

Timer Failed! [Simple][@lua/weapons/gmod_tool/stools/cannon.lua (line 126)]

Any thoughts?

Anyone?

Change

if ent:IsValid() then

to

if IsValid( ent ) then

a variable can be nil, thus making :IsValid() useless unless you do if ent and ent:IsValid(), however the function IsValid() does this.

Excellent, it works now without any errors! Many thanks to both of you for your help.