CollectionService Tag Not working as intended. Need help

What I’m trying to achieve

I’m sure all or most of you know about collectionservice. In summary it’s a way of assigning certain objects or things in your game to perform the same task without having the same script created multiple in each individual object.
What I’m trying to achieve is to make all items that I have tagged using the collectionservice to perform the same task without having to put the same script in each individual item.

What is the issue?

I have created a collectionservice and tagged a certain item in my game and set a task for it. When I run the game the script works fine. But when I try duplicating the item the script doesn’t work for the newly cloned items. I’m not quite sure why… I even clicked on each individual cloned item and it shows that they are also tagged but it doesn’t run ;-;
image

What solutions have I tried so far?

Well, I’ve tried going on youtube to see if anyone had similar problems and if anyone had found a solution to it but it seems that no one has talked about it yet. Could someone please let me know what I’m doing wrong?

1 Like

First you need to create seperate function first (since i don’t know your code i will made up an example)
in my example i made three part in workspace called “TaggedPart” these part will have Tag FadePart tag on it

then i create function to make it perform task (make it fade in my case)

local CollectionService = game:GetService("CollectionService")
local TweenService = game:GetService("TweenService")
local function DoFade(Part)
	Part.Transparency = 0
	TweenService:Create(Part, TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1,true), {Transparency = 1}):Play()
end

now get all tagged part and call function to perform task on each tagged part

for _, TaggedPart in ipairs(CollectionService:GetTagged("FadePart")) do
	DoFade(TaggedPart)
end

then last thing is track all new part that have the tag added by using CollectionService:GetGetInstanceAddedSignal()
(Clone part will also fire this function)

then give the new part the same task function

CollectionService:GetInstanceAddedSignal("FadePart"):Connect(function(TaggedPart)
	DoFade(TaggedPart)
end)

my final code

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

local function DoFade(Part)
	Part.Transparency = 0
	TweenService:Create(Part, TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1,true), {Transparency = 1}):Play()
end

for _, TaggedPart in ipairs(CollectionService:GetTagged("FadePart")) do
	DoFade(TaggedPart)
end

CollectionService:GetInstanceAddedSignal("FadePart"):Connect(function(TaggedPart)
	DoFade(TaggedPart)
end)

(I am not very good at explaining thing so if you confused feel free to ask me)

1 Like

very simple way of doing this

when the game starts you got two options

1: when the game starts find all the objects with a certain tag (loop through all descendants of workspace)

2: option 1 and when the game starts connect a instance added function

code exampel:

local CollectionService = game:GetService("CollectionService")

local tagScript = script.TagScript

-- option 1
for i, v in pairs(CollectionSerive:GetTagged("Tag")
    local s = tagScript:Clone()
    s.Disabled = false
    s.Parent = v
end

-- option 2
for i, v in pairs(CollectionSerive:GetTagged("Tag")
    local s = tagScript:Clone()
    s.Disabled = false
    s.Parent = v
end

CollectionService:GetInstanceAddedSignal("Tag"):Connect(function(v)
    local s = tagScript:Clone()
    s.Disabled = false
    s.Parent = v
end)

I’m a bit confused with option 1 and option 2 because it seems like they both do the same task but yes I finally see what I was doing wrong. But not gonna lie the part where you put v in the CollectionService:GetInstanceAddedSignal(“Tag”):Connect(function(v) threw me off. But it just turns out that the parenthesis just represents the item that’s been cloned.

option 2 is for repeating of tagged instances being added and the first thing in option 2 is just in case the script loads a bit late and it missed a few tagged instances

oh I get what you mean! Thanks for the clarification

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.