How would i add a delay or 'cooldown' for a clickdetector?

hi! i have an object that a player needs to click in order for an event to happen, how would i make it so that theres a “cooldown” and they can only click it once before the object destroys?

You could use wait() to make a cooldown I guess.

1 Like

where do i put the script again? also, does it work for a local script?

Hm um depends on what you will use the cooldown for and how. If its the script gave you when you tocuhed the doll it dissapears you could put a wait() before transparency is set to 1

1 Like

OOPS i meant to ask this for the other post LOL

User interfaces (mouse, keyboard, etc) only work in LocalScripts.

i posted that reply on the wrong post, i’m sorry!

Just add a debounce?

local Part = script.Parent
local DB = false

local function RemoveObject()
    local DestroyCheck = workspace:FindFirstChild("YourPartNameHere")

    if not DB and DestroyCheck then
        DB = true
        print("The debounce has been set to true!")
        DestroyCheck:Destroy()

        wait(5)

        DB = false
        print("The debounce has been set to false!")
    end
end

Part.ClickDetector.MouseClick:Connect(RemoveObject)
1 Like

(late sorry) what does the last part for? “Part.ClickDetector.MouseClick:Connect(RemoveObject)”

It connects an event. Events are like signals that will fire when certain conditions are met.

In this event, it will fire when the ClickDetector receives a mouse click, hence it’s called a “MouseClick” event. Connect is used here to connect the event to a function; when the Event is fired, it will run the function.

mhm, but why is there a clicked event at the bottom? what is it used for in the debounce

You are asking a cooldown when a ClicKDetector is clicked, right?

This script tells us that the MouseClick event will only register if it is not currently in cooldown.

local doll2 = game.Workspace.dolls.CHAIR1doll
local sound = doll.Parent.Sound
local one = game.Lighting.one
local two = game.Lighting.two
local number = game.Workspace.numberSeven.seven
local Part = script.Parent
local DB = false

one.Enabled = false	
two.Enabled = false
number.Transparency = 1



doll.MouseClick:Connect(function()
	local function RemoveObject()
		local DestroyCheck = workspace:FindFirstChild("doll1")

		if not DB and DestroyCheck then
			DB = true
			print("The debounce has been set to true!")
			DestroyCheck:Destroy()

			wait(5)

			DB = false
			print("The debounce has been set to false!")
		end
	end

	Part.ClickDetector.MouseClick:Connect(RemoveObject)
	sound:Play()
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)

	local part = script.Parent   --part, our our doll

		local playersService = game:GetService('Players')    -- emmber players isa service and we have to get that
		local player = playersService:FindFirstChildOfClass('Player')    --Were finding the player
		local leaderstats = player:FindFirstChild("leaderstats")   --the leaderstats we made 
		local dolls = leaderstats:FindFirstChild('Dolls') --Our dolls number
		dolls.Value = dolls.Value + 1 --Adds 1 to our doll count
	for i,v in pairs(workspace.dolls.doll1:GetDescendants()) do
		if v:IsA("BasePart") or v:IsA("Decal") then	
			v.Transparency = 1
			if v.Transparency == 1 then
				doll:Destroy()
			end
			for i,v in pairs(workspace.dolls.CHAIR1doll:GetDescendants()) do
				if v:IsA("BasePart") or v:IsA("Decal") then
					v.Transparency = 0
					number.Transparency = 0
					end
				end
			end
		end
end)```

here is my script (ignore how messy it is), this made the whole thing not work, is it in the wrong place?

Ok what is this

one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)
	one.Enabled = true
	wait(.03)
	one.Enabled = false
	wait(.03)
	two.Enabled = true
	wait(.03)
	two.Enabled = false
	wait(.03)

Maybe use a loop? Introduction to Scripting | Roblox Creator Documentation

like i said just ignore the mess bc im not too worried abt that rn, im really focused on fixing this debounce.

The debounce should work, if you want the loop, here it is:

for i = 0, 20, 1 do -- Will repeat 20 times
    one.Enabled = true
    task.wait(.03)
    one.Enabled = false
    task.wait(.03)
    two.Enabled = true
    task.wait(.03)
    two.Enabled = false
    task.wait(.03)
end

great, the loop works, but the debounce still does not. i put it under the mouseclick function with the one/two loop and now its like there isnt a debounce at all and i can still spam click the thing

If the one/two loop running is what you mean by “doesn’t work”, it’s because it just runs automatically, the Part.ClickDetector.MouseClick:Connect isn’t stopping it, especially if no one clicks the detector. You can put the entire thing in the RemoveObject function in the debounce if statement

im sorry its 4 am and im so tired and confused

the debounce does not work, i can still spam click the thing before it destroys