5 min read

Source: Roblox Creator Hub · CC BY 4.0 · View source · Code samples: MIT Imported 2026-10-03. Formatting adapted for this site.

Collection

A Collection is a live, query-based group of instances created by CollectionService:CreateCollection() using a selector string and an optional root instance. From then on, membership is automatic such that an instance is added the moment it matches the query and removed the moment it stops matching. You never add or remove instances manually.

You can interact with a Collection by assigning callbacks to it. Rather than connecting a signal for each instance and remembering to disconnect it, you declare behavior once on the collection and it is applied to every matching instance for you. The collection owns those connections and tears them down automatically when an instance leaves the set. See the accompanying code sample for a collection that uses each kind of callback together.

A collection's lifetime is tied to the root instance passed to CreateCollection() (defaulting to Workspace). When that root is destroyed or when you call Destroy(), the collection is torn down.

Callback Assignment Syntax

Callbacks are assigned as fields on the collection using function-statement syntax, which reads like the contents of a ModuleScript. Both of the following forms are equivalent and assign through the collection's overloaded __newindex:

local CollectionService = game:GetService("CollectionService")

local collection = CollectionService:CreateCollection("Part")

function collection.OnAdded(instance)

end
local CollectionService = game:GetService("CollectionService")

local collection = CollectionService:CreateCollection("Part")

collection.OnAdded = function(instance)

end

Each callback can be assigned only once; reassigning the same callback raises an error. This keeps a collection's behavior declarative at its definition so that it cannot be silently replaced elsewhere.

Core Members and Aliases

OnAdded, OnRemoved, and ForEach() are the collection's primitive members:

A collection can also bind a callback to non-deprecated RunService steps through aliases which exist for performance as well as convenience:

Collection Callback Equivalent
OnHeartbeat Class.RunService.Heartbeat
OnPreRender Class.RunService.PreRender
OnPreAnimation Class.RunService.PreAnimation
OnPreSimulation Class.RunService.PreSimulation
OnPostSimulation Class.RunService.PostSimulation
OnSimulate Class.RunService:BindToSimulation()

The collection subscribes to each RunService event once and dispatches to its members internally, rather than every instance holding its own connection. The callback fires once per step for each instance in the collection and receives the instance followed by the step's deltaTime:

local CollectionService = game:GetService("CollectionService")

local collection = CollectionService:CreateCollection("Part")

function collection.OnHeartbeat(instance, deltaTime)

end

Event, Property, and Attribute Wrappers

Beyond the RunService aliases, a collection can wrap any signal on its member instances. In these callbacks, X stands for the name of an event, property, or attribute.

Note that a single collection can match instances of different classes, so a wrapped signal may not apply to every member:

Typing and Autocomplete

When the query constrains the class of its matches, the collection is typed accordingly and callbacks are typed to match. For example, CreateCollection("Part.TagName") yields a collection whose callbacks receive a Part, and autocomplete only suggests callbacks relevant to that class (so collection.OnTouched(part: Part, other: BasePart) is suggested, but not for a query that cannot match a BasePart). When no type can be inferred from the query, the first argument defaults to Instance.

Code samples: View on Creator Hub (Collection-Overview).

Properties

NameType / ReturnsDescription
Collection.EnabledboolControls whether the collection is actively tracking instances and firing callbacks.

Collection.Enabled

Defaults to true. Setting this to false fires OnRemoved for every instance currently in the collection and stops all of its callbacks. Setting it back to true re-evaluates the query and fires OnAdded again for every matching instance. Assigning a non-boolean value raises an error.

FieldValue
typebool

Methods

NameType / ReturnsDescription
Collection:ForEachInvokes a function once for each instance currently in the collection.
Collection:DestroyDestroys the collection, stopping all tracking and callbacks.

Collection:ForEach

Iterates over the instances currently in the collection, calling callback with each one. ForEach() operates on a snapshot taken at call time, so it is safe to add, remove, or destroy instances from within the callback; instances that begin matching during iteration are not visited. Calling ForEach() after the collection has been destroyed raises an error.

Parameters

NameTypeDefaultDescription
callbackfunctionA function called with each instance currently in the collection.

Collection:Destroy

Stops firing the collection's callbacks, releases the connections it manages on its members, and empties its set of instances. After a collection is destroyed, calling ForEach() raises an error. A collection is also destroyed automatically when the root passed to CreateCollection() is destroyed.