Help with CollectionService

Topic

Hi, I’m Sven and I’m currently learning to script. I want to try making several parts spin; with just one script to reduce lag. So I did some research and I read the article about CollectionService. I decided to try and use the CollectionService and make it work this way.

What did I do?

So I tagged with the help of the Tag Plugin the needed parts and then inserted a normal script inside of the ServerScriptService. The script goes as the following:

local CollectService = game:GetService("CollectionService")
local TaggedParts = CollectService:GetTagged("StarTag")

for _, TaggedPart in pairs(TaggedParts) do
	
Spin = 0
repeat
TaggedPart.Rotation = Vector3.new( 0, Spin, 0) 
wait(.01) 
Spin = Spin+5

until x == 1
		
end

But…

The tagged parts somehow do not spin, and I personally don’t understand why. I can’t make them work and I hope you can help me with what I did wrong.

Extra information

If you can help me, thank you so much! Feel free to ask anything, I really need help.

I’m pretty sure I didn’t see you reference the x variable until this line. You should do something with this in order to spin your parts.

Hi, well the x == 1 is made so it will loop forever.
This part is not the problem, because it does spin when I insert it inside of a part and make it script.Parent instead of TaggedPart; if you get what I mean.

Thank you anyway!
Sven

What? If your parts are spinning, then your issue is already solved…?

No, because I want it to spin with using CollectionService.

Are you sure that you tagged all of the parts first?

I tagged them after, does that matter?

Yes because it won’t be able to find any tagged parts if you did it after

I really don’t understand your code, but you can try:

for _,part in pairs(TaggedParts) do
    local y += 1
    part.Rotation = Vector3.new(0,y,0)
    wait(.01)
end

I will try this one.
Thank you for your suggestion, I will let you know if it works.

That’s already the issue, you must tag them BEFORE you do anything with it.

lol, I’m not really good at scripting. Thank you, I will use this instead of the other.

I believe the issue is because of the repeat until loop along with the wait() which yields the script so that it only spins one part.

To fix this you can create a seperate thread to prevent the yield from stopping the for loop.

local CollectService = game:GetService("CollectionService")
local TaggedParts = CollectService:GetTagged("StarTag")

for _, TaggedPart in pairs(TaggedParts) do
	coroutine.wrap(function()
		Spin = 0
		repeat
			TaggedPart.Rotation = Vector3.new( 0, Spin, 0) 
			wait(.01) 
			Spin = Spin+5
		until x == 1
	end)()	
end

It still doesn’t seem to work, any of the options…
I have no idea how to be honest.

Thank you all anyway.

Are all of the parts you want to spin in a folder or are they scattered around?

If they are in a folder I recommend just iterating through the folder instead of using collection service.

They are scattered around in workspace.

What do you suggest me to do? I can put them in a folder if needed, but I don’t really know a lot about how folders work lol.

Are you creating new stars?

If so then you will need a collection service signal to connect the code to the new star:

Working sample
local CollectionService = game:GetService("CollectionService")
local TaggedParts = CollectionService:GetTagged("StarTag")

local function spinPart(part)
	coroutine.wrap(function()
		Spin = 0
		repeat
			part.Rotation = Vector3.new( 0, Spin, 0) 
			wait(.01) 
			Spin = Spin+5
		until x == 1
	end)()	
end

for _, TaggedPart in pairs(TaggedParts) do
	spinPart(TaggedPart)
end
CollectionService:GetInstanceAddedSignal("StarTag"):Connect(spinPart)

Also make sure you tag them in the first place using a plugin or manually by script:

And the code works fine for me, tested it in studio.

Proof of it working:

4 Likes
local CS = game:GetService("CollectionService")

for _, v in ipairs(workspace:GetDescendants()) do
    if v.Name == "Star" then
        CS:AddTag("StarTag")
    end
end

local TaggedParts = CS:GetTagged("StarTag")

for _, part in ipairs(TaggedParts) do
    -- spin star
end

Wow, such a detailed answer. I appreciate the time you’ve put into helping me! (same goes for the other people). I will test it and let you know how it worked out.

Thanks.

1 Like

Thank you very much, you have made my day!

Have a nice day, too.