API:Class/CollectionService

Filtered-inheritance mode: While this class technically inherits all Instance class members, some (if not all) have been hidden because they serve no purpose for this object.
Service: This class is a service. It is a top-level singleton which can be obtained with the GetService method.

CollectionService.pngCollectionService : Object Icon.pngInstance

A service which provides collections of instances based on tags assigned to them. Tags will replicate from the server to the client and in team create, and serialize when you save a place. However, if you set a tag on an instance from the client, and then set a different tag or remove a different tag on that same instance from the server, the client's local tags on that instance will be overwritten.

Functions

FunctionsmemberhiddenFunctions [toggle]

voidAddTag ( Instance instance, string tag )

Adds a tag to an instance.

|RMD member="API:Class/CollectionService/AddTag"|Adds a tag to an instance.|/RMD|
RBXScriptSignal<Instance>GetInstanceAddedSignal ( string tag )

Returns a signal that fires when the given tag either has a new instance with that tag added to the DataModel or that tag is assigned to an instance within the DataModel.

|RMD member="API:Class/CollectionService/GetInstanceAddedSignal"|Returns a signal that fires when the given tag either has a new instance with that tag added to the DataModel or that tag is assigned to an instance within the DataModel.|/RMD|
RBXScriptSignal<Instance>GetInstanceRemovedSignal ( string tag )

Returns a signal that fires when the given tag either has a new instance with that tag removed from the DataModel or that tag is removed from an instance within the DataModel.

|RMD member="API:Class/CollectionService/GetInstanceRemovedSignal"|Returns a signal that fires when the given tag either has a new instance with that tag removed from the DataModel or that tag is removed from an instance within the DataModel.|/RMD|
array<Instance>GetTagged ( string tag )

Returns an array of all of the instances in the DataModel which have the given tag.

|RMD member="API:Class/CollectionService/GetTagged"|Returns an array of all of the instances in the DataModel which have the given tag.|/RMD|
array<string>GetTags ( Instance instance )

Returns a list of all available instance tags.

|RMD member="API:Class/CollectionService/GetTags"|Returns a list of all available instance tags.|/RMD|
boolHasTag ( Instance instance, string tag )

Returns whether the given instance has the given tag.

|RMD member="API:Class/CollectionService/HasTag"|Returns whether the given instance has the given tag.|/RMD|
voidRemoveTag ( Instance instance, string tag )

Removes a tag from an instance.

|RMD member="API:Class/CollectionService/RemoveTag"|Removes a tag from an instance.|/RMD|
array<Instance>GetCollection ( string class ) [deprecated]

Deprecated in favor of GetTagged.

|RMD member="API:Class/CollectionService/GetCollection"|Deprecated in favor of GetTagged.|/RMD|
AddTag ( Instance instance, string tag )
Adds a tag to an instance.
Returns a signal that fires when the given tag either has a new instance with that tag added to the DataModel or that tag is assigned to an instance within the DataModel.
Returns a signal that fires when the given tag either has a new instance with that tag removed from the DataModel or that tag is removed from an instance within the DataModel.
Returns an array of all of the instances in the DataModel which have the given tag.
GetTags ( Instance instance )
Returns a list of all available instance tags.
HasTag ( Instance instance, string tag )
Returns whether the given instance has the given tag.
RemoveTag ( Instance instance, string tag )
Removes a tag from an instance.
GetCollection ( string class ) [deprecated]
Deprecated in favor of GetTagged.


Events

EventsmemberhiddenEvents [toggle]

ItemAdded ( Instance instance ) [deprecated]

Deprecated. Do not use.

|RMD member="API:Class/CollectionService/ItemAdded"|Deprecated. Do not use.|/RMD|
ItemRemoved ( Instance instance ) [deprecated]

Deprecated. Do not use.

|RMD member="API:Class/CollectionService/ItemRemoved"|Deprecated. Do not use.|/RMD|
ItemAdded ( Instance instance ) [deprecated]
Deprecated. Do not use.
ItemRemoved ( Instance instance ) [deprecated]
Deprecated. Do not use.

|RMD member="API:Class/CollectionService"|A service which provides collections of instances based on tags assigned to them. Tags will replicate from the server to the client and in team create, and serialize when you save a place. However, if you set a tag on an instance from the client, and then set a different tag or remove a different tag on that same instance from the server, the client's local tags on that instance will be overwritten. |/RMD|


Example

local CollectionService = game:GetService("CollectionService")
 
------------------------------------------------------------------------------ 
 
local Door = {}
Door.__index = Door
 
function Door.new(door)
    local self = {}
    self.door = door
 
    -- do something when a door is created
 
    return setmetatable(self, Door)
end
 
function Door:Destroy()
    -- do something when a door is destroyed
end
 
------------------------------------------------------------------------------ 
 
local doors = {}
 
local doorAddedSignal = CollectionService:GetInstanceAddedSignal("Door")
local doorRemovedSignal = CollectionService:GetInstanceRemovedSignal("Door")
 
local function onDoorAdded(door)
    doors[door] = Door.new(door)
end
 
local function onDoorRemoved(door)
    if doors[inst] then
        doors[inst]:Destroy()
        doors[inst] = nil
    end
end
 
for _,inst in pairs(CollectionService:GetTagged("Door")) do
    onDoorAdded(inst)
end
 
doorAddedSignal:Connect(onDoorAdded)
doorRemovedSignal:Connect(onDoorRemoved)