CollectionService

You will learn about CollectionService.

by PepsiHaus

Author Avatar

CollectionService Basics

Today you'll learn about using CollectionService

CollectionService manages groups of instances that are tagged.

The primary use of CollectionService is to register instances with specefic tags that you can use to extend their behavior.

An Example would be, let's say you want a kill brick. We could write a single script and put it in each part, but that would make it annoying to implement new features since we would have to copy the changes to each kill part and this is where CollectionService comes into play to make new changes without the hassle of changing each script for an instance.

First we'll need to create a Tag which we can do inside ROBLOX Studio, VIEW > Tag Editor

image|100x100

Create a tag named KillBrick and add it to a part

image|100x100

Then create a Server Script inside ServerScriptService and name it KillBrick and we'll be going through all the instances tagged with KillBrick.

local CollectionService = game:GetService("CollectionService")

local KillBricks = CollectionService:GetTagged("KillBrick")

for _, killBrick in ipairs(KillBricks) do

end

We'll now be able to add some logic when you touch the part.

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

local KillBricks = CollectionService:GetTagged("KillBrick")

for _, killBrick in ipairs(KillBricks) do
    if not killBrick:IsA("BasePart") then return end

    killBrick.Touched:Connect(function(hit: BasePart)
        local player: Player = Players:GetPlayerFromCharacter(hit.Parent)

        if not player then return end
        local humanoid: Humanoid = hit.Parent:FindFirstChild("Humanoid")
        humanoid.Health = 0
    end)
end

More with CollectionService

We'll also be able to do so if you add a tag during Runtime, you'll be able to add the logic to that tagged instance.

An example for this would be.

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

local KillBricks = CollectionService:GetTagged("KillBrick")

function OnTouched(hit: BasePart)
    local player: Player = Players:GetPlayerFromCharacter(hit.Parent)

    if not player then return end
    local humanoid: Humanoid = hit.Parent:FindFirstChild("Humanoid")
    humanoid.Health = 0
end

local Connections = {}

-- First we go through all the already tagged instances and add the logic
for _, killBrick in ipairs(KillBricks) do
    if not killBrick:IsA("BasePart") then return end
    Connections[killBrick] = killBrick.Touched:Connect(OnTouched)
end

-- Adding logic to a KillBrick that has been tagged during Runtime
CollectionService:GetInstanceAddedSignal("KillBrick"):Connect(function(instance: Instance)
    if not instance:IsA("BasePart") then return end
    Connections[instance] = instance.Touched:Connect(OnTouched)
end)

-- Remember to cleanup!
CollectionService:GetInstanceRemovedSignal("KillBrick"):Connect(function(instance: Instance)
    if Connections[instance] then
        Connections[instance]:Disconnect()
        Connections[instance] = nil
    end
end)
   

Thanks for reading, if you found this helpful it would be appreciated if you could react with helpful and else if something wasn't to your liking please tell me what and I'll see if I can change/fix it :)

View in-game to comment, award, and more!