I’m trying to add a cooldown using if statement on a UI Button, I’m currently self-learning lua and I have no idea why the cooldown doesn’t work
Current script
local canLoop = false
local Count = script.Parent.Parent.Parts
local PartText = script.Parent.Parent.PartText
script.Parent.MouseButton1Click:Connect(function()
-- Check that we are not already looping to avoid it running many times at once (aka, debounce)
if not canLoop then
-- Loop is started upon mouseclick as well as the loop
canLoop = true
print("Parts has been added in Workspace, loop is on")
script.Parent.Text = "Active"
while canLoop do
local part = Instance.new("Part",game.Workspace.Parts)
Count.Value += 1
part.Anchored = false
--PartText.Text = "Parts: "..Count
-- Funny wait() to avoid crash
wait()
end
else
canLoop = false
script.Parent.Text = "Disabled"
wait(1.5)
script.Parent.Text = "Low"
end
end)
Script using if statement as cooldown
local canLoop = false
local Count = script.Parent.Parent.Parts
local textButton = script.Parent
local cooldown = false
local PartText = script.Parent.Parent.PartText
script.Parent.MouseButton1Click:Connect(function()
-- Check that we are not already looping to avoid it running many times at once (aka, debounce)
if not canLoop then
if cooldown == true then
print("Cooldown is active")
else
canLoop = true
print("Parts has been added in Workspace, loop is on")
script.Parent.Text = "Active"
while canLoop do
local part = Instance.new("Part",game.Workspace.Parts)
Count.Value += 1
part.Anchored = false
wait()
end
end
else
canLoop = false
cooldown = true
if cooldown == true then
canLoop = false
cooldown = false
script.Parent.Text = "Disabled"
wait(1.5)
script.Parent.Text = "Low"
wait(0.5)
cooldown = false
end
end
end)
It would be very nice if someone can help me with this.