How to make this debounce better

Now currently my debounce looks like this:

local Debounce = false
function something(args)
	repeat task.wait() until Debounce == false
	Debounce = true
	--Code here...
	print(args)
	task.wait(1)
	Debounce = false
end

I’m using a repeat until loop, so that if i call the function two times quickly after eachother, they will both print instead of only one printing

However this doesn’t really work well, because if i call the function multiple times with different args, it doesn’t print in the same order I called it.

it really depends on what u want to achieve. Mostly people do it like this

local function foo()
    if debounce then return end
    debounce = true

    print("hi")
    task.wait(1)
    debounce = false
end
1 Like

This wont work, because if i call the function like this

foo() -- enables debounce
task.wait(0.1)
foo() -- this wont do anything now, which is not what i want

Yes, that is because once the first function sets the debounce to false, everything else also detects it and reacts to it. I dont see why you would want it to be like this, but since you do, you should use some sort of queue system instead.

Have a table ready for use, and assign each called function to the currently last item of the table (which the function should create). Use a variable to make sure that the function has the correct item. As you now have the list of debounces, only change the first debounce which is currently in the list. Use another repeat loop to check if the previous value is nil and if it is the first value. Each function should wait until its previous value is nil to move its value up (and changing the variable as well), Once the function realizes it is the first debounce (as in, the variable is now one), you now want to start the actual task.wait() function.

This is really messy but I just woke up so I dont think I can explain better

1 Like

did u ran the code. task.wait will eventually yield the code enabling debounce again

1 Like

I will try this solution! thank you for the reply

They want the debounce to be a queue rather then a real debounce, as in, it cant just return, but should instead wait until debounce is true

this doesn’t work in my case, you see, this function can also be called from different scripts, i tried the script but it simply doesn’t work

Thank you for this solution, altough i kind of gave it my own twist, I guess?
Final script:

local Queue = {}
function module.gui.Notice(Player: Player, Format: string, Text: {}, IsNext)
	if IsNext then -- when calling the function normally, isnext is nil
	elseif IsNext == nil then table.insert(Queue, {Player, Format, Text}) return end
	print("something")
end
local function QueueNotice()
	while true do
		task.wait()
		if #Queue ~= 0 and Queue[1] ~= nil then
			local Args = Queue[1]
			module.gui.Notice(Args[1], Args[2], Args[3], true) -- calls the function but with isnext set as true
			table.remove(Queue, 1)
		end
	end
end
coroutine.wrap(function()
	QueueNotice()
end)()
2 Likes

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