How do I make a proper cooldown for my spawner?

Hello Dev’s!

I made a script that is supposed to spawn 1 part every 180 seconds once a player clicks the button.

the issue is, players that have a weak internet connection or an auto clicker can spawn 2 or 3 part before the 180 seconds cooldown starts,

the script is supposed to change the max distance of the click detector to 0 until the cooldown finishes, but it doesn’t function in time.

image

Note: I made it spawn a part for the sake of making an example.

this is the script that is supposed to spawn the part:

    local box = script.Parent
    local clicky = script.Parent:WaitForChild("ClickDetector")


    function Hit()
    	clicky.MaxActivationDistance=0
    	script.Parent.BrickColor = BrickColor.new("Really red")
    	
    	
    	local part = Instance.new("Part")
    	part.Parent = game.Workspace
    	part.Position = Vector3.new(0,10,0)
    	
    	
    	for i = 180 , 0 , -1 do
    		
    		script.Parent.Parent.WaitNumber.SurfaceGui.Text.Text = (""..i)
    		wait(1)
    	end
    	clicky.MaxActivationDistance=30
    	script.Parent.BrickColor = BrickColor.new("Lime green")
    	
    end

     script.Parent.ClickDetector.MouseClick:connect(Hit)

Just add a debounce system in your Hit() function:

    local Hitting = false
    function Hit()
        -- Debounce
        if Hitting then
            return
        end
        
        Hitting = true

    	clicky.MaxActivationDistance=0
    	script.Parent.BrickColor = BrickColor.new("Really red")
    	
    	
    	local part = Instance.new("Part")
        for i = 180 , 0 , -1 do
    		
    		script.Parent.Parent.WaitNumber.SurfaceGui.Text.Text = (""..i)
    		wait(1)
    	end
    	clicky.MaxActivationDistance=30
    	script.Parent.BrickColor = BrickColor.new("Lime green")
    	
        -- They can hit it again!
        Hitting = false
    end
2 Likes