Help with CollectionService

So, I have a CollectionService tag that has 4 objects.

local collectionService = game:GetService("CollectionService")
local videoFramesTag = collectionService:GetTagged("VideoFrames")

for i, v in (videoFramesTag) do
    print("Hello") -- Prints 8 times
end

The issue is that it loops 8 times. Shouldn’t it loop 4 times because the tag has 4 objects?

2 Likes

somehow, this is not working it is only printing 4 times for me

3 Likes

Figured it out, it’s because the code was written in a LocalScript, not a ServerScript.

That’s still weird though. It loops x2 times when written inside a LocalScript

1 Like

Is the runcontext set to client? It may run inside the starterCharacterScript, then get cloned again and run twice inside a cloned local script in the player character.

Same thing if its inside starterGUi as the script gets cloned. Seen a similar problem on #help-and-feedback:scripting-support some time this week.

4 Likes

Same as above, you may also at some point use LoadCharacter or replace the player’s character, this causes it to work twice

3 Likes

That’s because it is in starterCharacterScripts or StarterGui, and it ran before it was cloned, resulting in the loop running 8 times. you should probably put to starterPlayerScripts instead, if the character is not needed. The starterGui also clones it to the playerGui which is outside of playerscripts. so the localscript will run

2 Likes

No, set to Legacy.

It’s within StarterGui, not StarterPlayerScripts nor StarterCharacterScripts.

1 Like

Something that could affect StarterGui? Do you use LoadCharacter? Do you remove the Character in some way?

1 Like

that’s only if the ScreenGui has Reset on Spawn on, but if it does then it will run once again.

1 Like

Not necessarily, its parent may be StarterGui and not a ScreenGui, but it may also be that.

2 Likes

No, I’m not manipulating the character in any way, shape or form.

1 Like

Fixed it by using for i = 1, #screenGui:GetChildren() do instead.

1 Like