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.
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.
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
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:
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.