Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Bullets are creating explosions twice when hitting multiple parts?

I'm pretty confused on the workaround for this. My gun's bullets, when hitting multiple parts, runs the touched event twice before the bullet is destroyed.

	missile.Touched:connect(function(hit)
		if hit.CanCollide ~= false or hit.Name == "Right Leg" or hit.Name == "Left Leg" or hit.Name == "Left Arm" or hit.Name == "Right Arm" then
			if hit.Transparency ~= 1 then -- The above could lag?
				missile.Parent = nil -- Still creates the explosion
				local E = Instance.new("Explosion")
				E.BlastPressure = S.ExplosionPressure
				E.BlastRadius = S.ExplosionRadius
				E.DestroyJointRadiusPercent = (S.RangeBasedDamage and 0 or 1)
				E.ExplosionType = S.ExplosionType
				E.Position = missile.Position -- It creates two explosions!!!!
				E.Parent = game.Workspace
				E.Hit:connect(function(hitPart, partDistance)
					if (not hitPart:IsDescendantOf(Character)) and hitPart.Name == "Torso" then
						print("Hit "..hitPart.Name.."!")
				    	local humanoid =  hitPart.Parent:FindFirstChild("Humanoid")
				    	if humanoid then
					        local distance_factor = partDistance / E.BlastRadius  
					        local distinvert = 1 - distance_factor
							local newdamage = damage*distinvert 
					        humanoid:TakeDamage(newdamage) 
				    	end
					end
				end)
				
				--HitSound:Play()
				
			end
		end
	end)

So, due to this, it creates two explosions, thus harming any humanoids nearby twice. How would I prevent this from happening?

2 comments

1

Try missile:Destroy() instead of missile.Parent = nil and after you test it report it here please.

0

It works! Thanks!

1 answer

What you could do is say that if the script is in the bullet. If not then change script.parent to where the bullet is.

script.Parent.Touched:connect(function(hit)

Then tell the script that after the bullet hits something to stop giving explosions.

This discussion is preserved as it was. New answers, comments, and votes are closed.