CollectionService - how to add tag to multiple parts?

Hi!,
I try to use Collection Service (without plugins), add Tags to multiple parts etc.
Questions are:
How can I add Tags to the value of 3rd variable (“local aC = aCLoaded:GetChildren()”) wich is a table automatically?
How can I get the “return” value (from this table) and us it later for example also with Collection Service (“HasTag”, “GetTagged” etc…)?

local CollectionService = game:GetService(“CollectionService”)

local aCLoaded = game.Workspace:WaitForChild(“AC”)

local aC = aCLoaded:GetChildren()

local tag = “ACTAG”

local function onTouch(otherPart)

if not otherPart.Parent then return end

local humanoid = otherPart.Parent:FindFirstChildOfClass(“Humanoid”)

if humanoid then

humanoid.Health = humanoid.Health - 20

end

end

local connections = {}

local function onInstanceAdded(object)

if object:IsA(“BasePart”) then

connections[object] = object.Touched:Connect(onTouch)

end

end

local function onInstanceRemoved(object)

if connections[object] then

connections[object]:disconnect()

connections[object] = nil

end

end

CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)

for _, object in pairs(CollectionService:GetTagged(tag)) do

onInstanceAdded(object)

end

And sorry for my english grammar :slight_smile: Thank you advice!

You can use AddTag(), which needs individual instances, so you need to loop through:

local children = parent:GetChildren()

for _, inst in pairs(children) do

    CollectionService:AddTag(inst, "SomeTag")

end
1 Like