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.
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
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)()