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

is there a way to make the doll destroy as soon as i click it

Wait so do you want a cooldown or not??

1 Like

i do but also its really just so that people cant keep clicking it to play the flickering animation and stuff over and over before the doll destorys, so if there was a way to make the doll destroy as soon as its clicked, that would work too

So you want to destroy the doll immediately and wait until the flickering and the doll destruction? If that’s what you want, you can make the debounce go false after the whole thing instead of just the

DB = true
print("The debounce has been set to true!")
DestroyCheck:Destroy()
wait(5)
DB = false

So maybe like

doll.MouseClick:Connect(function()
    if not DB then
        -- all the code
        DB = false
    end
end)
local doll = game.Workspace.dolls.doll1.ClickDetector
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()
	if not DB then
		sound:Play()
		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

		local function RemoveObject()
			local DestroyCheck = workspace.dolls: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



		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

		DB = false
	end)

i have this, it still does not work

You must enable the Debounce first. Like this:

DB = true

Put this under if not DB then.

doll.MouseClick:Connect(function()
	if not DB then
        DB = true
		sound:Play()
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()
	if not DB then
		DB = true
		sound:Play()
		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

		local function RemoveObject()
			local DestroyCheck = workspace.dolls:FindFirstChild("doll1")

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

				wait(30)

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



		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

		DB = false
	end)

i have this and it does not work still

Don’t reply to year-old topics, please. There’s a high chance that they already solved their problem and abandoned this post.

Yeah I edited my comment stating that the topic is dead and it’s probably useless. :innocent:

How did you even manage to find this and reply to it?

I was going around tick() posts on the devforums, stumbled upon this somehow xD

1 Like

Try to use
local ClickCooldown = false
local cooldown = 5 --place cooldown here
clickdetector.MouseClick:connect(function)
if ClickCooldown == false then
You script
ClickCooldown = True
wait(cooldown)
ClickCooldown = false
end
–I haven’t tested it but it should work