local ents.Create - NULL entity

Hello,

So I’m trying to remove an entity once created, this is example code I’m using:


function ENT:Initialize()

	self:SetModel( "models/props_building_details/Storefront_Template001a_Bars.mdl" )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	self:SetUseType( SIMPLE_USE )

	local phys = self:GetPhysicsObject()

	if( IsValid(phys) ) then
		phys:Wake()
		phys:SetMass(30)
	end

	self.Opened = true

end

function ENT:Use( activator, caller )
	if caller:IsPlayer() and caller:IsValid() then

		if( self.Opened ) then
			self.Opened = false

			timer.Simple( 0.25, function()
				local newprop = ents.Create( "prop_physics" )
				newprop:SetModel( "models/props_c17/fence01b.mdl" )
				newprop:SetPos( self:GetPos() )
				newprop:SetAngles( self:GetAngles() )
				newprop:SetMoveType( MOVETYPE_NONE )
				newprop:PhysicsInit( SOLID_NONE )
				newprop:SetSolid( SOLID_VPHYSICS )
				newprop:Spawn()

				local phys = newprop:GetPhysicsObject()

				if( IsValid(phys) ) then
					phys:Wake()
					phys:EnableMotion(false)
				end
			end)
		elseif( !self.Opened ) then
			newprop:Remove()

			self.Opened = true
		end

	end
end

I know it’s to do with the local variable, but if I remove that then if I spawn more than one and use them it will mess up and not work properly because it’s Global.
Any help would be appreciated.

Error is:


[ERROR] addons/guy/lua/entities/doors/init.lua:49: Tried to use a NULL entity!
  1. Remove - [C]:-1
   2. unknown - addons/guy/lua/entities/doors/init.lua:49


This is the newprop:Remove() line

Just declare the variable outside then?



function ENT:Use( activator, caller )
	if caller:IsPlayer() and caller:IsValid() then

		local newprop -- This sets it to nil for now

		if( self.Opened ) then
			self.Opened = false

			timer.Simple( 0.25, function()
				newprop = ents.Create( "prop_physics" )
				newprop:SetModel( "models/props_c17/fence01b.mdl" )
				newprop:SetPos( self:GetPos() )
				newprop:SetAngles( self:GetAngles() )
				newprop:SetMoveType( MOVETYPE_NONE )
				newprop:PhysicsInit( SOLID_NONE )
				newprop:SetSolid( SOLID_VPHYSICS )
				newprop:Spawn()

				local phys = newprop:GetPhysicsObject()

				if( IsValid(phys) ) then
					phys:Wake()
					phys:EnableMotion(false)
				end
			end)
		elseif( !self.Opened ) then
			newprop:Remove()

			self.Opened = true
		end

	end
end


For fucks sake, why am I always dumb…
It’s always something obvious.

Thanks man… solved.