Tried to use a NULL value! (Timer Failed!)

So I am modifying this vehicle health script, yes I know I didn’t make it, but I am changing it to my likings. In doing so I have hit a problem I can’t seem to figure out. The only thing I really changed was that first the vehicle ignits, then it gives you the chance to put it out and save your vehicle, and if you don’t it exlopdes and disappears. I don’t have TDM, just using it on jeep and airboat (in case you were wondering). I have made it work, except when it removes the vehicle, it begins to spam the server console, not even clients. Here is the code and the error:
[LUA]
local DefaultVehicleHP = 300
hook.Add(“EntityTakeDamage”, “VehicleHPHook”, function( ent, dmginfo )
if not ent:IsVehicle() then
return
end
if not ent.HP then
ent.HP = DefaultVehicleHP + math.random(50,200)
end
ent.HP = ent.HP - dmginfo:GetDamage()
if ent.HP <= 0 and not ent.Destroyed then
local ExplodeTime = math.random(10,30)
ent:Ignite(ExplodeTime,100)
if ent:GetDriver():IsPlayer() then
ent:GetDriver():ExitVehicle()
end
timer.Simple(ExplodeTime, function()
if not ent then return end
local explosion = ents.Create(“env_explosion”)
if ent:IsOnFire() then
ent.Destroyed = true
ent:SetColor(Color(0,0,0,255))
explosion:SetPos(ent:GetPos())
explosion:SetOwner(attacker)
explosion:Spawn()
explosion:SetKeyValue(“iMagnitude”, “200”)
explosion:Fire(“Explode”, 0, 0)
ent:Extinguish()
if ent:GetDriver():IsPlayer() then
ent:GetDriver():Kill()
end
timer.Simple(5, function()
ent.Destroyed = false
ent:Remove()
ent.HP = DefaultVehicleHP
end)
elseif !ent.Destroyed and ent then
ent.HP = DefaultVehicleHP - math.random(50,100)
else
return
end
end)
end
end)

hook.Add(“CanPlayerEnterVehicle”, “ExplodedVehicles”, function(ply, vehicle, role)
if vehicle.Destroyed then
return false
end
end)
[/LUA]

Like I said, there is no issues with it working, works great! The error I get spammed though it:

[LUA]
[ERROR] lua/autorun/server/vehiclehealth.lua:19: Tried to use a NULL entity!

  1. IsOnFire - [C]:-1
  2. unknown - lua/autorun/server/vehiclehealth.lua:19
    Timer Failed! [Simple][@lua/autorun/server/vehiclehealth.lua <line 16>]
    [/LUA]

line 19:
[LUA] if ent:IsOnFire() then [/LUA]

line 16:
[LUA] timer.Simple(ExplodeTime, function() [/LUA]

I have tried a lot of different things and can’t get it to go away without making it not work… Can anyone help? Thanks in advance!

Make sure the entity is valid before you try to use it.

.-. I’m not sure why I didn’t do that in the first place. It’s been a long day XD and only getting longer. I might have another post with a completely different issue soon. Thanks for helping, works great.