How to add a debounce into a collection service?

I’m trying to learn about Collection Service. I’ve added tags to three different “popcorn makers” called PopcornMaker. In the loop I made below, I added a function to each popcorn maker’s click detector. Everything works well, I just need to add a debounce to it so that the player can’t click it constantly in a certain timeframe. I’m confused as how to do this though, can someone please help me or give me clues on what to do?

for i, popcornMaker in pairs(popcornMakers) do
	popcornMaker:FindFirstChild("ClickDetector").MouseClick:Connect(function(player)
		local textLabel = popcornMaker:FindFirstChild("BillboardGui").TextLabel
		debounce = true
		
		coroutine.wrap(function()

			for count = 15, 0, -1 do
				textLabel.Text = "Countdown: "..tostring(count).."s"
				task.wait(1)
			end

			textLabel.Text = "Click this block to make popcorn!"
			debounce = false
		end)()

		for count = 10, 0, -0.1 do
			player.leaderstats.Cash.Value += 10
			wait(0.1)
		end
	end)
end

Ignore the debounce I added in the script, I forgot to remove it :sweat_smile:

you mean how to add a debounce into a mousedetector?

local waitDebounce = 1
for i, popcornMaker in pairs(popcornMakers) do
    local debounce = false
	popcornMaker:FindFirstChild("ClickDetector").MouseClick:Connect(function(player)
		local textLabel = popcornMaker:FindFirstChild("BillboardGui").TextLabel
        if debounce then return end
		debounce = true
		
		coroutine.wrap(function()

			for count = 15, 0, -1 do
				textLabel.Text = "Countdown: "..tostring(count).."s"
				task.wait(1)
			end

			textLabel.Text = "Click this block to make popcorn!"
		end)()
		for count = 10, 0, -0.1 do
			player.leaderstats.Cash.Value += 10
			wait(0.1)
		end
        wait(waitDebounce)
        debounce = false
	end)
end

Yup, I meant that, sorry for the confusion. Thank you for the answer it works like a charm now. Just one more question, so anything I put in the for loop, even all the stuff I put outside of the MouseClick function would be transferred to the popcorn maker?

yeah basically

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.