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
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)
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
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!