Entity always null after being spawned even with timer delay

Imma trying to make the player blow up into fizzling gibs but it keeps erroring out!

What am I doing wrong???

ERROR:



[ERROR] gamemodes/cage/gamemode/init.lua:137: Tried to use a NULL entity!
  1. SetKeyValue - [C]:-1
   2. unknown - gamemodes/cage/gamemode/init.lua:137

Timer Failed! [Simple][@gamemodes/cage/gamemode/init.lua (line 134)]



LUA:
[LUA]
function GM:PlayerDeath( victim, item, pl )

local velocity = victim:GetVelocity()/2
victim:GetRagdollEntity():Remove() 
local explode = ents.Create( "env_explosion" )
explode:SetPos( victim:GetPos() )
explode:SetOwner( victim )
explode:Spawn()
explode:SetKeyValue( "iMagnitude", "50" )
explode:Fire( "Explode", 0, 0 )
explode:EmitSound( "npc/env_headcrabcanister/explosion.wav", 100, 100 )

local gibs = {
	"alpha_gib_leftleglower.mdl",
	"alpha_gib_rightleglower.mdl",
	"alpha_gib_leftlegupper.mdl",
	"alpha_gib_rightlegupper.mdl",
	"alpha_gib_waist.mdl",
	"alpha_gib_tummy.mdl",
	"alpha_gib_body.mdl",
	"alpha_gib_head.mdl"
}

local gibsspawned = {}                                             
local count = 0

for k,v in pairs(gibs) do
	local gib = ents.Create( "prop_physics" )
	gib:SetModel( "models/player/alpha/gib/" .. v )
	gib:SetPos( Vector( victim:GetPos().x, victim:GetPos().y, victim:GetPos().z+16 ) )
	gib:SetAngles( Angle( math.random( -7, 7 ), math.random( -7, 7 ), math.random( -7, 7 ) ) ) 
	gib:SetCollisionGroup( COLLISION_GROUP_DEBRIS )
	gib:SetColor( team.GetColor( victim:Team() ) )
	gib:SetKeyValue( "targetname", "gibdis" .. count )
	gib:Spawn()
	gib:Activate()
	gib:GetPhysicsObject():SetVelocity( velocity )
	gibsspawned[count] = gib
	count=count+1
end

local dissolver = ents.Create( "env_entity_dissolver" )
	dissolver:SetPos( victim:GetPos() )
	dissolver:Spawn()
	dissolver:Activate()
	dissolver:SetKeyValue( "magnitude", 100 )
	dissolver:SetKeyValue( "dissolvetype", 1 )

timer.Simple( 5, function()                                                          -------(Line 134)-------
	for k,v in pairs(gibsspawned) do
		dissolver:SetKeyValue( "target", v:GetName() )
		dissolver:Fire( "Dissolve" )                                          -------(Line 137)-------
	end
end )
dissolver:Remove()

end
[/LUA]

I cleaned your code up a bit. Your problem was that you were removing the dissolver before the timer was executed, and not performing any validity checks in the timer.


local gibs = {
	"models/player/alpha/gib/alpha_gib_leftleglower.mdl",
	"models/player/alpha/gib/alpha_gib_rightleglower.mdl",
	"models/player/alpha/gib/alpha_gib_leftlegupper.mdl",
	"models/player/alpha/gib/alpha_gib_rightlegupper.mdl",
	"models/player/alpha/gib/alpha_gib_waist.mdl",
	"models/player/alpha/gib/alpha_gib_tummy.mdl",
	"models/player/alpha/gib/alpha_gib_body.mdl",
	"models/player/alpha/gib/alpha_gib_head.mdl"
}

function GM:PlayerDeath( victim )
	local velocity = victim:GetVelocity()/2
	victim:GetRagdollEntity():Remove() 
	local explode = ents.Create( "env_explosion" )
	explode:SetPos( victim:GetPos() )
	explode:SetOwner( victim )
	explode:Spawn()
	explode:SetKeyValue( "iMagnitude", "50" )
	explode:Fire( "Explode", 0, 0 )
	explode:EmitSound( "npc/env_headcrabcanister/explosion.wav", 100, 100 )
	
	local gibsspawned = {}                                             
	local count = #gibs
	local pos = victim:GetPos()
	pos.z = pos.z + 16
	
	for i = 1, count do
		local gib = ents.Create( "prop_physics" )
			gib:SetModel( gibs* )
			gib:SetPos( pos )
			gib:SetAngles( Angle( math.random( -7, 7 ), math.random( -7, 7 ), math.random( -7, 7 ) ) ) 
			gib:SetCollisionGroup( COLLISION_GROUP_DEBRIS )
			gib:SetColor( team.GetColor( victim:Team() ) )
			gib:SetKeyValue( "targetname", "gibdis" .. i )
			gib:Spawn()
			gib:Activate()
			gib:GetPhysicsObject():SetVelocity( velocity )
		gibsspawned* = gib
	end
	
	local dissolver = ents.Create( "env_entity_dissolver" )
		dissolver:SetPos( victim:GetPos() )
		dissolver:Spawn()
		dissolver:Activate()
		dissolver:SetKeyValue( "magnitude", 100 )
		dissolver:SetKeyValue( "dissolvetype", 1 )
	
	timer.Simple( 5, function()
		if ( dissolver:IsValid() ) then
			for i = 1, count do
				local gib = gibsspawned*
				
				if ( gib:IsValid() ) then
					dissolver:SetKeyValue( "target", v:GetName() )
					dissolver:Fire( "Dissolve" )
				end
			end
			
			dissolver:Remove()
		else
			for i = 1, count do
				local gib = gibsspawned*
				
				if ( gib:IsValid() ) then
					gib:Remove()
				end
			end
		end
	end )
end

It fixed one of my errors! Thank you!