No Free Edicts = No More Patience

I worked pretty damn hard to get a some swep code working only to find that at the end of it all, when the entire thing seems to be working fine, Gmod started crashing and occationally the No Free Edicts error message box would appear. Note that when I disable the ents.FindInSphere, the code doesn’t crash gmod.

Here it is (this is called during a swep primary fire hook):
[lua]
function CatalystEffect(self)
self.looprar = self.looprar + 1
self.activetargets = 0
local culla = 255-(5.1 * self.looprar)

for k,ent in pairs(self.Deathents) do

if ent:IsValid() then
ent:SetColor(culla,culla,culla,255)
self.activetargets = self.activetargets + 1

if self.looprar== 50 and (SERVER) then

  local explode = ents.Create("env_ar2explosion")
  local pos = ent:GetPos()
  explode:SetPos(pos)
  explode:Spawn() 
  explode:SetKeyValue("iMagnitude","0") 
  explode:Fire("Explode", 0, 0 ) 
  explode:EmitSound("weapon_AWP.Single", 400, 400 ) 

			
	local pos = ent:GetPos()
	ent:TakeDamage(1000)
	if ent:IsValid() then
	ent:Remove()
	end
		
		
	for k,entt in pairs(ents.FindInSphere(pos,100)) do
		if !entt:IsPlayer() then
		self.increm = self.increm + 1
		self.Deathents[self.increm] = entt
	end
					
			end

end
end
end

if self.looprar == 50 then
self.looprar = 0
end

if self.activetargets == 0 then
timer.Stop(“CatalystDeath”)
end

end
[/lua]

Super Side Note:
How do I make NPC’s attack prop-based entities?

No free edicts usually means that there are too much entities in game.

It finds your explosion entity, iterates again, creates a new explosion entity, finds that, iterates again etc etc. Eventually there has been too many explosion entities created and you crash.
To fix it add a check so that it doesn’t add the explosion entity to self.Deathents

Ah, I get what you are saying. I always forget that entities such as effects get picked up by FindInSphere.

Thanks for the help, I’ll try to fix it now.

I’ll give this a go tomorrow. If this works then thanks for the help.

I’m not positive on this but, I think you create an info_target entity, Set it at the props position( I think you might also have to parent it to the prop), and then use NPC:SetEnemy(your_info_target_entity)