CollectionService:GetTagged returning 3x the number of expected items

Hi, I’m currently struggling with some issues when using collection service, perhaps I’m not understanding some basics. For example, I have two parts in my workspace named FlagPole tagged as “Flag”. A red Flag and a blue Flag.

However if I run the following:
for i,flag in pairs(collectionService:GetTagged(“Flag”)) do

it finds a total of 6 instances of FlagPole tagged as “Flag”. I can even check explorer when this is happening and confirm only two flagpole’s exist in the workspace- there’s no cloning or anything.

I have assigned each of those flagpole Parts to a variable and then add that variable as part of an array, it seems like collection service is then considering those as separate instances?
local objectives = {blueFlag, redFlag}

If I run that loop now collection service says 4 flagpoles. If I pass that objective array into a function then collection service finds 6 flags now…

I thought assigning a variable to a part would just essentially make a pointer and not a separate instance??

It gets tagged Instances in the entire game, i.e. also ServerStorage and everywhere else. Probably also Instances whose Parent is nil. Try this:

for _, t in ipairs(TagS:GetTagged(daTag)) do
    print(t:GetFullName()) --prints the entire path to the instance, e.g. game.Workspace.Folder.Thing
1 Like

thank you! I didn’t realize collection service would also account for serverStorage- what’s weird is when i manually stepped through each “Flag” it found and manually checked the parent on the “watch” tab it appeared it was finding more flags only in workspace.

However using GetFullName I was able to find the apparent culprit- there was a mapSave function running at the start which put a copy of the map in serverStorage, including any flags. After cleaning that up and putting a “IsDescendantOf(workspace)” check I think it’s working as I expected now!

1 Like