Skip to content
Engine

Data

Collections

Queue, Stack, Deque, PriorityQueue, RingBuffer, LRUCache, Set and Multiset with constant-time operations.

PackageShared

Getting it

local Collections = Engine:GetPackage("Collections")

Overview

Plain Luau tables are great for arrays and dictionaries, but some jobs need a data structure with different guarantees: removing from the front of a list without shifting it, always getting the smallest item, or forgetting the oldest entries automatically. Collections provides those, each with a small consistent API.

All collections have a read-only Size and IsEmpty. Popping or peeking an empty collection raises an error, so check IsEmpty first when it might be empty.

local Collections = Engine:GetPackage("Collections")

local queue = Collections.Queue()
queue:Push("first")
queue:Push("second")
print(queue:Pop()) --> first

local recent = Collections.RingBuffer(10) -- keeps the last 10 entries
for i = 1, 25 do
	recent:Push(i)
end
print(recent:Oldest(), recent:Newest()) --> 16 25

Queue

Collections.Queue(items: { any }?): Queue

First in, first out. Push adds to the back, Pop and Peek use the front.

MemberDescription
:Push(value)Add to the back.
:Pop(): anyRemove and return the front item.
:Peek(): anyReturn the front item without removing it.
:Clear()Remove everything.
:ToArray(): { any }Items from front to back.

Stack

Collections.Stack(items: { any }?): Stack

Last in, first out. Same members as Queue, but Pop and Peek use the most recently pushed item.

Deque

Collections.Deque(items: { any }?): Deque

A double-ended queue: add and remove at both ends in constant time.

MemberDescription
:PushFront(value), :PushBack(value)Add at an end.
:PopFront(), :PopBack()Remove and return from an end.
:PeekFront(), :PeekBack()Read an end.
:Clear(), :ToArray()As above.

PriorityQueue

Collections.PriorityQueue(less: ((a: number, b: number) -> boolean)?): PriorityQueue

Always hands out the item with the lowest priority number first (a binary heap). Pass your own less to change the order, for example function(a, b) return a > b end for highest first.

local jobs = Collections.PriorityQueue()
jobs:Push("save", 2)
jobs:Push("kick cheater", 1)
local job, priority = jobs:Pop() --> "kick cheater", 1
MemberDescription
:Push(value, priority: number)Add an item.
:Pop(): (any, number)Remove and return the first item and its priority.
:Peek(): (any, number)Read the first item and its priority.
:Clear()Remove everything.

RingBuffer

Collections.RingBuffer(capacity: number?): RingBuffer

A fixed-size buffer that overwrites its oldest item once full. Ideal for "the last N" of anything: chat lines, frame times, positions. Capacity defaults to the DefaultRingBufferCapacity setting.

MemberDescription
Capacity: number, IsFull: booleanRead-only.
:Push(value)Add an item, dropping the oldest if full.
:Get(index): anyItem by age, 1 is the oldest. Errors when out of range.
:Oldest(), :Newest()The oldest and newest item.
:Clear(), :ToArray()ToArray returns oldest to newest.

LRUCache

Collections.LRUCache(capacity: number?): LRUCache

A key-value cache that evicts the least recently used entry when it is full. Capacity defaults to the DefaultLRUCacheCapacity setting.

local thumbnails = Collections.LRUCache(200)

thumbnails.Evicted:Connect(function(userId, image)
	print("dropped", userId)
end)

local function getThumbnail(userId)
	local cached = thumbnails:Get(userId)
	if cached then
		return cached
	end
	local image = Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size100x100)
	thumbnails:Set(userId, image)
	return image
end
MemberDescription
Capacity: numberCan be changed; shrinking it evicts entries. Must be at least 1.
Evicted: Signal<(key, value)>Fires when an entry is pushed out.
:Get(key): anyThe value, or nil. Marks the entry as recently used.
:Peek(key): anyThe value without touching its recency.
:Set(key, value)Insert or replace, marking it recently used.
:Has(key): boolean, :Remove(key): boolean, :Clear()

Set

Collections.Set(items: { any }?): Set

A collection of unique items.

MemberDescription
:Add(item): booleantrue if the item was new.
:Remove(item): booleantrue if the item was there.
:Has(item): boolean, :Clear(), :ToArray()
:Union(other), :Intersect(other), :Difference(other), :SymmetricDifference(other)Return a new Set.
:IsSubsetOf(other), :IsSupersetOf(other), :IsDisjointFrom(other)Comparisons.

Multiset

Collections.Multiset(items: { any }?): Multiset

A set that counts how many of each item it holds, like an inventory of stackable items.

MemberDescription
Size: numberTotal count of all items.
UniqueCount: numberNumber of different items.
:Add(item, amount?): numberAdds amount (default 1), returns the new count.
:Remove(item, amount?): numberRemoves amount (default 1), returns the new count.
:Count(item): number, :Has(item): boolean
:ToArray()Every item, repeated by its count.
:UniqueItems()Every different item once.

Configuration

Defaults from DefaultConfiguration/Packages/Collections2 settings
SettingDefaultWhat it does
DefaultRingBufferCapacity64Slots a RingBuffer.new() gets when no capacity is passed.
DefaultLRUCacheCapacity128Entries an LRUCache.new() holds when no capacity is passed.