How to make explosion stop repeating

How would I make my explosion only explode ONCE?

This is my current code.
It explodes 56 times instead of one, which causes lag for the game.

wait(1)
local folder = workspace.MineFolder

while true do
	wait(1)
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA('BasePart') then
			v.Touched:Connect(function(touched)
				
				local possibleHumanoid = touched.Parent:FindFirstChildWhichIsA('Humanoid')
				if possibleHumanoid then
					possibleHumanoid.Health = 0
					v.Transparency = 0
					local explosion = Instance.new("Explosion", v)
					explosion.Position = v.Position
					explosion.BlastRadius = 2
					explosion.BlastPressure = 35
					print("EXPLOSION!")
				end
			end)
		end
	end
end

image

1 Like

You want it to explode once (if it detects a humanoid), right? If so, you can just disconnect the .Touched() connection.

wait(1)
local folder = workspace.MineFolder

while true do
	wait(1)
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA('BasePart') then
			v.Touched:Connect(function(touched)
				
				local possibleHumanoid = touched.Parent:FindFirstChildWhichIsA('Humanoid')
				if possibleHumanoid then
					possibleHumanoid.Health = 0
					v.Transparency = 0
					local explosion = Instance.new("Explosion", v)
					explosion.Position = v.Position
					explosion.BlastRadius = 2
					explosion.BlastPressure = 35
					print("EXPLOSION!")
				end
			end)
		end
	end
end

use a debounce

local debounce = true

script.Parent.Touched:Connect(function()
if debounce == true then
debounce = false
-- explosions and stuff --

-- if you want to re-enable it --
debounce = true

end

Didnt work, still gave me 42 explosions.

Probably because you are continously looping the .Touched connection (because of while true do). Why do you need to loop in the first place?

This didnt work either, it still made 56 explosions.

wait(1)
local folder = workspace.MineFolder

local debounce = true

while true do
	wait(1)
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA('BasePart') then
			v.Touched:Connect(function(touched)
				if debounce == true then
					debounce = false
					local possibleHumanoid = touched.Parent:FindFirstChildWhichIsA('Humanoid')
					if possibleHumanoid then
						possibleHumanoid.Health = 0
						v.Transparency = 0
						local explosion = Instance.new("Explosion", v)
						explosion.Position = v.Position
						explosion.BlastRadius = 2
						explosion.BlastPressure = 35
						print("EXPLOSION!")
					end
					debounce = true
					
				end
			end)
		end
	end
end

It needs to be looped so that it make kills the player.
Refer to recent thread here

try not re-enabling the debounce at the end.

2 Likes

This did work!

Thank you @Downrest and @lukethebestpug !

1 Like

my main concern is that once a player touches it and explodes, it won’t happen again, basically disabling that function once connected.

I made sure, it does explode again. Not sure how or why but I wont question it

lol, ok then

[character limit]