| Download Model |
Although queues in ROBLOX tend to be useful for niche cases I use this module frequently and have included it in other resources so I thought I’d make it a standalone resource and provide documentation.
This module allows the creation of queues using any datatype as keys and can run functions back to back (hence a queue) instead of at the same time. It is useful for storing code to run at a later time or for features in your game that you do not want to run simultaneously but want to run eventually, otherwise unexpected behavior would occur- examples of such cases include usually include making batch changes to a model’s parts such as: loading mesh part outfits on characters, modifying all joints/constraints in a model, or in the case of my resource VoxelDestruct processing destructive actions on the same part.
If your queue is meant to modify an instance, like a model/character/part, I recommend using the instance as the key.
If you use this for anything cool please share!
Documentation
Create a Queue
Queue.New(Object: any, MassRun: boolean?)
Create a queue with any data type as a key. Ignore the MassRun
parameter as it hasn’t been implemented yet.
Queue.Fetch(Object: any)
To retrieve an existing queue elsewhere, fetch it by passing its key.
Methods
Queue:Add(func: Function, waits: boolean?, run: boolean?)
Adds a function to the queue.
waits: boolean?
Optionally yield until the added function has been run by the queue.
run: boolean?
Optionally trigger the queue to run.
Queue:Run(waits: boolean?)
Starts the queue if it is not already running.
waits: boolean?
Optionally yield until the queue has completed.
Queue:Clear(RunFunctions: boolean?)
Will clear the queue.
RunFunctions: boolean?
Optionally run functions in the queue before clearing.
Queue:Pause()
Pause the queue if it is currently running.
Queue:Resume()
Resume the queue if it is currently paused.
Events
.Completed()
Fires when the queue has completed.
Properties
Queue.Paused
Whether or not the queue is paused.
Queue.IsRunning
Whether or not the queue is active.
Example
local key = "MyQueueNameHere" -- Can be any datatype or any instance
local Queuer = require(game:GetService("ReplicatedStorage"):WaitForChild("Queue"))
local myQueue = Queuer.Fetch(key) or Queuer.New(key) -- Ensures only one queue exists for this key
myQueue:Add(function()
print("Hello")
end)
myQueue:Add(function()
print("World!")
end, true, true) -- Yield until this function has run, and run the queue automatically so calling :Run() is not necessary.
print("Finished.\n")
myQueue:Add(function()
print("More features to come!")
end)
myQueue:Add(function()
print("Comment your suggestions, please!")
end)
task.wait(3) -- Wait before running queue although functions have been added.
print("Running queue again...")
myQueue:Run() -- Run the queue.
--[[
Output:
Hello
World!
Finished.
(3 second wait seen here)
Running queue again...
More features to come!
Comment your suggestions, please!
]]