It’s pretty simple to make things happen one after the other. Here’s an example module you can use:
local Queue = {}
Queue._mt = {__index = Queue}
-- create a new Queue object, which can be used to run things one after the other.
function Queue.new()
local self = setmetatable({}, Queue._mt)
self.pendingTasks = {}
self.running = false
return self
end
-- queue a function to run later
-- if no other functions are currently queued, this will run immediately
-- if other functions are queued, this will run after all other functions have completed execution
function Queue:QueueTask(callbackFunc)
table.insert(self.pendingTasks, callbackFunc)
if not self.running then
self:Run()
end
end
-- internally called to start processing all pending tasks
function Queue:Run()
self.running = true
spawn(function()
while #self.pendingTasks > 0 do
local task = table.remove(self.pendingTasks, 1)
task()
end
self.running = false
end)
end
return Queue
(wrote that just now so forgive me if there’s any errors )
To use that module, just call Queue.new
to create a new queue. Every queue object keeps its own internal list of pending tasks, so you can use multiple queue objects at the same time if you like. To queue a function to run, you can call :QueueTask(func)
on your queue object (func = the function to run).
Here’s a working example:
local function foo()
print("Waiting for 2 seconds...")
wait(2)
end
local function bar()
print("mayo")
end
local q = Queue.new()
q:QueueTask(bar)
q:QueueTask(foo)
q:QueueTask(bar)
In the output, you should see mayo
and Waiting for 2 seconds...
, and then after 2 seconds you’ll see another mayo
.
So when you recieve your remote events, you can just :QueueTask()
straight away. Then, the functions will be forced to run one after the other, even if you get two events at the same time.
Hope this helps!