Debounce not stopping the loop

Im trying to make a cooldown when a new round starts this cooldown should stop meteorites from spawning and then wait 20 seconds to enable the meteor spawn loop
heres the code:

local s = game:GetService("ServerStorage")
local wp = game:GetService("Workspace")
local sg = script.Parent.RegenControl.Signal
local str = "Meteors incoming, stay away from these fireballs!"
local w = 140
local debounce = false
debounce = true
sg.Changed:Connect(function()
	for _, p in pairs(wp:GetChildren()) do
		if p:IsA("Model") and p.Name == "Dup" then -- destroy leftover meteors as new round is starting
			p:Destroy()
		end
		debounce = false -- act like turning off a switch to stop the while wait loop
		print("stopping rocks from spawning")
		wait(20)
		debounce = true -- turn on switch again to start the while wait loop again
		end
end)
if debounce == true then -- checks when switch is on then meteor should spawn every 20 seconds
	print("started")
	while wait(20) do
		local h = Instance.new("Hint")
		h.Text = str
		h:Clone()
		local d = s.Dup:Clone()
		h.Parent = wp
		d.Parent = wp
	end
end
--[[ problem is the debounce isnt working to check the loop for stopping and starting again

Any help?

1 Like

I’m not 100% sure on your code but from what I can see, your script is only checking if debounce = true when the script starts.

As the posts above said, you are only checking once outside the scope of the loop.

try changing the last part of your code to this and see.


while debounce do
	wait(20)
	local h = Instance.new("Hint")
	h.Text = str
	h:Clone()
	local d = s.Dup:Clone()
	h.Parent = wp
	d.Parent = wp
end

1 Like

still not work, the best i can do is disable the script and enable again

sg.Changed:Connect(function()
	for _, p in pairs(wp:GetChildren()) do
		if p:IsA("Model") and p.Name == "Dup" then -- destroy leftover meteors as new round is starting
			p:Destroy()
		end
		script.Disabled = true 
		print("stopping rocks from spawning")
		game.RunService.Stepped:Wait()
		script.Disabled = false 
		end
end)

Try to do this:

print("started")
while wait(20) do
	if debounce == true then -- checks when switch is on then meteor should spawn every 20 seconds
		local h = Instance.new("Hint")
		h.Text = str
		h:Clone()
		local d = s.Dup:Clone()
		h.Parent = wp
		d.Parent = wp
	end
end
Full code
local s = game:GetService("ServerStorage")
local wp = game:GetService("Workspace")
local sg = script.Parent.RegenControl.Signal
local str = "Meteors incoming, stay away from these fireballs!"
local w = 140
local debounce = false
debounce = true
sg.Changed:Connect(function()
	for _, p in pairs(wp:GetChildren()) do
		if p:IsA("Model") and p.Name == "Dup" then -- destroy leftover meteors as new round is starting
			p:Destroy()
		end
		debounce = false -- act like turning off a switch to stop the while wait loop
		print("stopping rocks from spawning")
		wait(20)
		debounce = true -- turn on switch again to start the while wait loop again
		end
end)
print("started")
while wait(20) do
	if debounce == true then -- checks when switch is on then meteor should spawn every 20 seconds
		local h = Instance.new("Hint")
		h.Text = str
		h:Clone()
		local d = s.Dup:Clone()
		h.Parent = wp
		d.Parent = wp
	end
end
--[[ problem is the debounce isnt working to check the loop for stopping and starting again
1 Like

Hello! I’m not really an expert, but in my scripts, debounces work only if you put above an if statement that checks whether the debounce is true or false. So to explain more smoothly:

for _, p in pairs(wp:GetChildren()) do
		if p:IsA("Model") and p.Name == "Dup" then
			p:Destroy()
		end
       if debounce == true then -- add another if statement that checks debounce.
		debounce = false 
		print("stopping rocks from spawning")
		wait(20)
		debounce = true
        end
		end

I hope this works, if anything goes wrong, ill look up again!

1 Like

It works! thanks very much for your help! :grin: this is also my first time using debounce but hey it worked! have a great day

1 Like

My pleasure to help! It took quite long to learn this for me tbh, i’m glad it worked.

1 Like