Debouncing with :Once()?

Is it better, worse, or just as good to debounce a signal with :Once() instead of using a debounce variable?

local clickDetector = Instance.new("ClickDetector") -- path to ClickDetector

local action
action = function()
	print("click detected!")
	task.wait(0.5)
	clickDetector.MouseClick:Once(action)
end

clickDetector.MouseClick:Once(action)

Honestly, it looks cleaner but I’m not sure if it’s worth changing habits.

4 Likes

I actually believe this is easier! Never seen this before, didn’t even know Once() existed. Tested it out myself too and it actually just saves some work.

It’s a very interesting way to debounce without having to create a local variable. Unless there’s an underlying performance issue with :Once(), I think this is just up to preference.

1 Like

While interresting, I think this still wouldn’t scale well because you’d be creating duplicate RBXScriptSignals everytime you connected with Once, not to mention how akward the upvalue switching looks.

What I do is usually:

local Debounce: boolean = false

local function Reset(): ()
	Debounce = false
end

Event:Connect(function(): ()
	if Debounce then
		Debounce = true
		task.delay(3, Reset)
	end
	print("Hi!")
end)

But if you’d like to, you can use this class I made awhile back that modularizes the process. Probably overkill for your case, but useful for big codebases.

local Debounce = Cooldown.new(2)

Event:Connect(function(): ()
	if Debounce:CanPass() then
		Debounce:Start()
	end
	print("Hi!")
end)
1 Like

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