Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Debounce function doesn't seem to do anything?

I have a function to handle debounces in its own separate module that is referenced inside my main framework module and can be referenced at any time.

Here is the debounce code from the module:

function Debounce.DebounceFunc(func)
	local isRunning = false
	return function(...)
		if isRunning then return end
		isRunning = true
		func(...)
		isRunning = false
	end
end

Here is a snippet of code where the debounce function is being used in a touched event. The problem is, the debounce doesn't seem to do anything, and the event is fired many times a second:

function Fire:Burn()
	self:Tick()
	self.Parent.Touched:connect(Framework.Debounce(function(hit)
		print(true)
	end))
end

Just in case people ask, here is the snippet of code in my framework module referencing the debounce module:

-- Server
		
if not MainFramework.LocalPlayer then
	MainFramework.Debounce = MainFramework.Modules.Debounce.DebounceFunc
end

Should also note that the framework handles the requiring of the modules so that isn't an issue either.

Any tips appreciated, can't tell if there's just some incorrect logic, or if I'm applying the function to the event wrong or what.

1 comment

0

Touched events are horrible and can clump threads together so tightly that they can bypass a debounce. If you really need an accurate touched event, then go with region3 instead and run it through a loop. If you're worried about resource consumption, you can always mitigate when the loop for the region3 fires.

3 answers

Your third line in the deBounce module returns the value immediately without doing anything, maybe that's the problem?

1 comment

0

actually it is creating a new function which is being returned, this new function is what is called and handles the debounce

This should help :)

local Debounce, Delay = true, 1.4 -- durration
Debounce = true

if Debounce then
	Debounce = false
	"Code"
	Debounce = true
end

3 comments

0

thanks, but i already know how to do this. it doesn't really answer my question at all. the module is created so my code is more efficient and i don't have to keep reusing that same code over and over again. also, my function practically uses this same logic, and it still doesn't work.

0

I dont understand the Problem, could you better explain?

0

Don't get me wrong, but I think "Debounce" only works on one script; unless you use a bindable event or remote event

There’s no wait in your DebounceFunc function so this is expected behaviour, as soon as func has ran (which is a very short duration), the denounce is toggled back, the debounce has negligible effect. You should try either hardcoding a wait value or passing a number along with the func function which controls the time after which the debounce is toggled. It simply isn’t yielding long enough.

1 comment

0

Thanks for letting me know. There was another section of my DebounceFunc function that utilised a wait, but I left that part out as I didn't think it would be necessary for this. I will try try this now

This discussion is preserved as it was. New answers, comments, and votes are closed.