[Module] debounce

First of all, yes: Modules which handle debounces already exist
However some of them are just over-complicated in solving a simple task

So that is why I came up with my own module


Usage

It’ll automatically handle a debounce on the first argument given
Whenever a return is true, the module applies a debounce right after

(examples)




The 1st argument is NOT optional, on that :HasTag, :AddTag and :RemoveTag need to be usable (Mandatory)

The 2nd and 3rd arguments are optional (order doesn’t matter, as seen in the exampels)
If one of them is a string it will be used as tag and if it’s a number it will be used as await

  • tag is a string which will be used in giving your current debounce a name
  • await is the duration in seconds for how long the debounce should occur

If you don’t specify tag or/and await the default values are set as following:
image


functions


Demo

(with 1 second as wait time)


Full Script Example
local Replicated = game:GetService("ReplicatedStorage") 
local player = game:GetService("Players").LocalPlayer

local debounce = require(Replicated.handleDebounce)

script.Parent.TextButton.MouseButton1Click:Connect(function()
	if (debounce:handle(player, 1, "exampleTag")) then
		print("clicked")
	else
		warn("debounced")
	end
end)

Repo: Baseplate.rbxl (49.7 KB)
Module: https://www.roblox.com/library/14183717209/handleDebounce

4 Likes

Is this Server sided or Client sided?

1 Like

There is a problem with this module, which is that it doesn’t wait for the block to finish:

script.Parent.TextButton.MouseButton1Click:Connect(function()
	if debounce(player, 3, "ExampleTag") then
		task.wait(5)
	else
		warn("debounced")
	end
end)

Also, using tags seems unnecessary, why can’t you just use a table?

The 3 you put is the amount of time the debounce will last. Look at their example script

It’s a module. Can be used anywhere.

the module is both server and client sided

The if-statement would only be true if the instance has no active debounce. Which means if you spam click, it would only trigger every 3 seconds since the time set for the debounce is 3

If you are using the module with different purposes within the same script, you’d want to use tags in order to specify a unique debounce.

Also you aren’t forced to set a tag

(If it doesn’t, I’ll fix that. The idea of tags is the use of multiple debounces)

I know, in my example I provided a fixed wait. But in many scenarios, the amount of time you wait in the block is variable and so not appropriate for this module. But it’s quite simple to do a debounce anyway, so it seems a waste to have a module like this for something so simple when it only works half of the time.

What exactly only works half of the time?

As stated, if the contents of the block yields, this module is not suitable.
E.g:

local function SwingSword(special)
    if debounce(player, 1) then
        if special then
            wait(2)
            Hit() -- debounce is over prematurely
        else
            wait(0.5)
            Hit() -- debounce is reasonable
        end
    end
end

It would be much easier to use a traditional debounce, which can be done very easily.

1 Like

Yes, I just realized that. Thanks for the feedback.

I will come up with an idea, probably where you are able to use a function as an argument
‘debounce’ will return once the function ended.

No idea if that makes sense, I’ll try to push an update to the module soon!

Update

  • Calling debounce with a function

:handleFunction(instance, tag: string?)(function, …)

(If no tag is set, tag in your config module will be used)

Our function:
image

Calling (two examples, same result):

Video: https://streamable.com/8mzdg4
Repo: repo.rbxl (55.1 KB)