Blast Damage Problem


function ENT:Explode()
	local cpos = self:GetPos()
	local effectdata = EffectData()
	effectdata:SetStart(cpos)
	effectdata:SetOrigin(cpos)
	effectdata:SetScale(1)
	util.Effect("Explosion", effectdata)
	
	util.BlastDamage(self, self, cpos, 3000, 80)
end


For some reason this causes the game/server to crash. I want to know why. Btw darks_money_printer is my name for the entity the explosion is coming from.
The util.BlastDamage line is what causes it.

Where are you calling ENT:Explode from? Also why are you using ents.FindByClass to search for one printer entity instead of simply using self?

This is my FIRST Gmod based Lua Script so I don’t know how to handle everything yet.

Also without the explosion damage it goes without a hitch doing what it should. It actually breaks with the blast damage in it.
I call it when the printer for example gets shot at.

Yeah there’s your problem. You told the printer to explode when damaged, creating an explosion which damages it further, causing it to explode, and so on in an infinite loop, that’s why the game freezes.

You can fix that by not calling Explode if the damage came from the printer itself, if you’re using the OnTakeDamage hook, it would look like this:

[lua]function ENT:OnTakeDamage(damage)
if damage:GetInflictor() ~= self then
self:Explode()
end
end[/lua]

Alternatively you can simply set a variable in Explode which prevents it from being called more than once:

[lua]function ENT:Explode()
– If this function hasn’t been called yet, self.Exploded will be nil
– because it hasn’t been set yet
if self.Exploded then
return
end

local cpos = self:GetPos()
local effectdata = EffectData()
effectdata:SetStart(cpos)
effectdata:SetOrigin(cpos)
effectdata:SetScale(1)
util.Effect("Explosion", effectdata)

util.BlastDamage(self, self, cpos, 3000, 80)

-- Set self.Exploded to true to prevent this function from creating an explosion more than once
self.Exploded = true

end[/lua]

Thanks I should have known it was an infinite loop causing it. It’s been a while since I touched lua so I wasn’t sure what was going on. :slight_smile: