kQueue - Reliably run functions in order

The kQueue module allows functions to be run in order reliably. Example use cases would be:

  • Data saving cycles: Save players data in order with delays between calls to avoid rate limits
  • Match/role queues: New players in a server would be added to the end of the queue to become the detective, for example
  • NPC action list: Different actions for an NPC to perform one after the other

This module is a remake of the Queue object I made for our game, Maldives Resorts, where it’s used for player data saving.

Standalone module:

Example usage
local kQueue = require(script.Parent)
local TestQueue = kQueue.new("AfterPrevious", 0.5)
-- Standard queue, functions run one after the other with .5s delay between calls

for i = 1, 10, 1 do -- Add 10 "print" calls, with two arguments each
	TestQueue:Add(print, "Current function in queue:", i)
end

TestQueue:Remove(5) -- Removes the 5th item added

print("# of remaining items in queue:", TestQueue:GetLength()) --  Should print 9

TestQueue:WaitFor(7)
print("Reached 7")

TestQueue.Cleared:Wait() -- Waits for queue cleared event
print("Finished!")

Warning:
While this module has proven very reliable in Maldives Resorts, I have marked it as v0.9 BETA as we have not tested every use case or scenario. Please report any inconsistencies or issues!

2 Likes