Ent explosion crashing server

I have the following code, when I shoot the enitity it explodes but 90% of the time it crashes the dedicated server im running it on. when it doesnt creates a huge explosion that makes a fucked texture on the ground almost like it had exploded 20 times in the same position.
I would like it to just explode and do a bit of damage to the player, anyway to fix this?


function ENT:OnTakeDamage(dmg)
    self:TakePhysicsDamage(dmg)
    if self.burningup then return end

    self.damage = (self.damage or 1) - dmg:GetDamage()
    if self.damage <= 1 then
        local rnd = math.random(1, 1)
        if rnd < 3 then

                local explosion = ents.Create("env_explosion");
                explosion:SetPos(self:GetPos());
                explosion:SetKeyValue("iMagnitude", "96");
                explosion:Spawn();
                explosion:Activate();
                explosion:Fire("Explode", 0, 0);
                local shake = ents.Create("env_shake");
                shake:SetPos(self:GetPos());
                shake:SetKeyValue("amplitude", "512");
                shake:SetKeyValue("radius", "256");
                shake:SetKeyValue("duration", "1.5");
                shake:SetKeyValue("frequency", "255");
                shake:SetKeyValue("spawnflags", "4");
                shake:Spawn();
                shake:Activate();
                shake:Fire("StartShake", "", 0);
                self:Remove()
		util.BlastDamage(self, self, self:GetPos(), 512, 50)

        end
    end
end


I think it might be because you’re calling



self:TakePhysicsDamage(dmg)


Wouldn’t that make the function call itself infinite times?

[editline]22nd March 2016[/editline]

Also, there’s not much point in doing



local rnd = math.random(1, 1)


Cause that’s always going to be below 3 (you were probably just doing that to test though)


function ENT:OnTakeDamage()


	local explosion = ents.Create("env_explosion");
explosion:SetPos(self:GetPos());
explosion:SetKeyValue("iMagnitude", "96");
explosion:Spawn();
explosion:Activate();
explosion:Fire("Explode", 0, 0);

local shake = ents.Create("env_shake");
shake:SetPos(self:GetPos());
shake:SetKeyValue("amplitude", "512");
shake:SetKeyValue("radius", "256");
shake:SetKeyValue("duration", "3");
shake:SetKeyValue("frequency", "255");
shake:SetKeyValue("spawnflags", "4");
shake:Spawn();
shake:Activate();
shake:Fire("StartShake", "", 0);
util.BlastDamage(self, self, self:GetPos(), 512, 50)
end


changed it to that and nothing changes

Perhaps the explosion is damaging the entity - perhaps try removing the entity FIRST, then running all that other stuff (sorry for these dumb replies but I can’t seem to figure out what’s wrong)

tried that doesn’t seem to change anything, here is the whole code perhaps its something else doing it?


AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:Initialize()

	self:SetModel("models/props_c17/clock01.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
  local colBlack = Color( 17, 17, 17, 255 )
  self:SetColor( colBlack )
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType( SIMPLE_USE )
	local phys = self:GetPhysicsObject()

	if phys:IsValid() then

		phys:Wake()
	end
end

function ENT:Touch( ply )
	self:Remove()
    ply:Kill()
    ply:EmitSound( Sound( "vo/npc/Barney/ba_pain03.wav" ), ply:GetPos(), 1, CHAN_AUTO, 1, 75, 0, 100 )
  local explosion = ents.Create("env_explosion");
explosion:SetPos(self:GetPos());
explosion:SetKeyValue("iMagnitude", "96");
explosion:Spawn();
explosion:Activate();
explosion:Fire("Explode", 0, 0);

local shake = ents.Create("env_shake");
shake:SetPos(self:GetPos());
shake:SetKeyValue("amplitude", "512");
shake:SetKeyValue("radius", "256");
shake:SetKeyValue("duration", "3");
shake:SetKeyValue("frequency", "255");
shake:SetKeyValue("spawnflags", "4");
shake:Spawn();
shake:Activate();
shake:Fire("StartShake", "", 0);
end


function ENT:OnTakeDamage()

self:Remove()
local explosion = ents.Create("env_explosion");
explosion:SetPos(self:GetPos());
explosion:SetKeyValue("iMagnitude", "96");
explosion:Spawn();
explosion:Activate();
explosion:Fire("Explode", 0, 0);

local shake = ents.Create("env_shake");
shake:SetPos(self:GetPos());
shake:SetKeyValue("amplitude", "512");
shake:SetKeyValue("radius", "256");
shake:SetKeyValue("duration", "3");
shake:SetKeyValue("frequency", "255");
shake:SetKeyValue("spawnflags", "4");
shake:Spawn();
shake:Activate();
shake:Fire("StartShake", "", 0);
util.BlastDamage(self, self, self:GetPos(), 512, 50)
end


Don’t forget to remove the entities you create. explosion:Fire(“kill”, 0, 0.1);

Entity.Remove only flags the entity for deletion on the next frame. The entity may still exist when the explosion hits even if Remove is called before the explosion is created.

I don’t quiet understand, do you mind giving me an example of what you mean ?

Removing the entity in the EntityTakeDamage function won’t actually remove the entity, so it will still infinite loop. A better way is to set a self.Exploded variable to true when it explodes and to only explode if self.Exploded isn’t true.

thank you thats fixed my problem