For loop goes through same instance twice

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to have for loop that goes through all the instances that are tagged with “CommandButton”.

  1. What is the issue? Include screenshots / videos if possible!

I only have one instance that is tagged with “CommandButton” but the for loop goes through two instances. And the path to instances is the same.

here is the output:
Output

Explorer:
Explorer

Code:

Tag Editor:
TagEditor

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block
local CollectionService = game:GetService("CollectionService")
local CommandBox = script.Parent
local InputBox = CommandBox:WaitForChild("InputBox")



for i,Button in ipairs(CollectionService:GetTagged("CommandButton")) do
	print("for loop activation "..tostring(i))
	print(Button:GetFullName())
end

CollectionService:GetInstanceAddedSignal("CommandButton"):Connect(function(Button)
	print("Event activation")
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

The problem is it’s looping through it but finding it in both the players PlayerGui and StaterGui, add a check:
if Button:IsDescendantOf("StarterGui") then continue end

So it should be:

-- This is an example Lua code block
local CollectionService = game:GetService("CollectionService")
local CommandBox = script.Parent
local InputBox = CommandBox:WaitForChild("InputBox")

for i,Button in ipairs(CollectionService:GetTagged("CommandButton")) do
     if Button:IsDescendantOf("StarterGui") then continue end
	print("for loop activation "..tostring(i))
	print(Button:GetFullName())
end

CollectionService:GetInstanceAddedSignal("CommandButton"):Connect(function(Button)
	print("Event activation")
end)