Explosion.Hit() not producing any output

I am trying to create an explosive projectile, with its resulting explosion dealing a scripted amount of damage that is dependent on the distance a player is from the position of the explosion and the explosion’s radius.

The hierarchy of the Tool that will fire this projectile is as follows:


Most of the functionality of the Tool is handled by a ModuleScript stored in ServerStorage which the RangedWeaponScript in the Tool requires from. The Projectile gets duplicated, parented into Workspace and the StandardProjectile Script (Highlighted) within the cloned Projectile will be enabled to handle events when the flying Projectile comes into contact with something.

Everything works as intended for the flying Projectile when it comes into contact with a surface with the StandardProjectile code except for one phenomenon which I can’t understand at all.

When the Projectile comes into contact with anything and its Explosive BoolValue is marked as True, the Projectile is meant to create an explosion at the point of contact. The excerpt of the StandardProjectile Script is as follows:

if (script.Parent.Explosive.Value) then
	--Explosion handler.
	local explosion = Instance.new("Explosion", workspace)
	explosion.BlastPressure = 0
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastRadius = 30
	explosion.Position = collisionEffectBlock.Position
	explosion.Hit:Connect(function(partHit, distance)
		print(partHit)
		print(distance)
	end)
	local exSound = script.Parent.ExplosionSound:Clone()
	exSound.Parent = collisionEffectBlock
	exSound.PitchShift.Octave = .9 + .2 * math.random()
	exSound.Volume = exSound.StandardVolume.Value * script.Parent.ExplosionAttack.Value / 25
	exSound:Play()
end

The resulting Explosion works fine and all, being able to push away unanchored Parts if the BlastPressure is set to anything greater than 0 and breaking joints if DestroyJointRadiusPercent is set to anything greater than 0. The ExSound plays too and the Explosion is positioned at the point of contact. However, no output seems to be printed out from explosion.Hit() for some very strange reason, even if the Explosion has an effect on Parts and joints in the Workspace.

To make sure my code was implemented correctly, I created a new Part with a ClickDetector in it, along with a simple Script, a child of the Part and therefore a sibling of the ClickDetector, as follows:

script.Parent.ClickDetector.MouseClick:Connect(function()
	local explosion = Instance.new("Explosion", workspace)
	explosion.BlastPressure = 0
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastRadius = 30
	explosion.Position = script.Parent.Position
	explosion.Hit:Connect(function(partHit, distance)
		print(partHit)
		print(distance)
	end)
end)

Upon clicking the Part with the ClickDetector, an Explosion is created and strangely enough, the output prints out every Part caught in the Explosion’s BlastRadius along with how far the Part was from the Explosion.

I am here to ask why the Explosion.Hit() event cannot be reproduced by my explosive projectile.

2 Likes

So I’ve looked into it again and did what I feel is a very roundabout way of solving the problem of the Explosion not registering anything through its Hit() event. But it works anyway.

First off, I create a new Part in ServerStorage called ExplosionPart. This ExplosionPart has a Script in it as follows:

script.Parent.AncestryChanged:Connect(function()
	local explosion = Instance.new("Explosion", workspace)
	explosion.BlastPressure = 0
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastRadius = 30
	explosion.Position = script.Parent.Position
	explosion.Hit:Connect(function(partHit, distance)
		print(partHit)
		print(distance)
	end)
end)

StandardProjectile was then modified as follows:

if (script.Parent.Explosive.Value) then
	--Explosion handler.
	local explosionPart = game.ServerStorage.Objects.ExplosionPart:Clone()
	explosionPart.Position = collisionEffectBlock.Position
	explosionPart.Parent = workspace
	game.Debris:AddItem(explosionPart, 5)
	local exSound = script.Parent.ExplosionSound:Clone()
	exSound.Parent = collisionEffectBlock
	exSound.PitchShift.Octave = .9 + .2 * math.random()
	exSound.Volume = exSound.StandardVolume.Value * script.Parent.ExplosionAttack.Value / 25
	exSound:Play()
end

What the projectile does when it comes into contact with a surface is to take the scripted Part from ServerStorage and then let that Part handle the Explosion’s dynamics from there. If the Explosion is near a lot of Parts, however, there will be a slight delay to print so much Parts that got affected by the Explosion. Making the Explosion’s BlastRadius be smaller should help.

I’m still confused as to why my initial approach did not work. Syntax seems right and all, no errors printed out either.

2 Likes