Cooldown using if statement

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.

In general, a typical cooldown (aka “debounce”) situation using conditionals looks something like this:

local debounce = false --Is the cooldown currently active?
script.Parent.MouseButton1Click:connect(function()
    if not debounce then
        debounce = true --Change this to true. Now the conditional on the previous line will be locked
        --
        --Do whatever action you want to have a cooldown here
        print("do action")
        --
        wait(1.5) --This is how long the cooldown will be active
        debounce = false --Revert the variable so the conditional can be unlocked again
    else
        --[[
            Optional else block. Could be used for cases where you want to inform the
            user that the current action is in cooldown
        --]]
        print("cooldown is active")
    end
end)

Why are you setting everything seperately?

You can do:
if not canLoop and cooldown == true then

And also,

Why are you setting the cooldown to false twice?

I had a copy of the script on the notepad, seems like I forgot to change it to true.