Collection Service help

Hello, I am a beginner scripter so this will probably be a silly mistake.

I want to make 3 parts rotate at once with only a single script inside ServerScriptService.
I tagged those parts using a tag editor plugin and i wrote the script but for some reason it is not working.

Here is the script.

local CollectionService = game:GetService("CollectionService")

local TaggedParts = CollectionService:GetTagged("Rotate")

for _, TaggedParts in pairs(TaggedParts) do
	while true do
		script.Parent.CFrame = script.Parent.CFrame*CFrame.fromEulerAnglesXYZ(0,0.02,0)
		wait()
	end
end

Thanks.

2 Likes

First of all, make sure your tag is exactly how it is when you get the tagged objects. It is case-sensitive. Also make sure you didn’t add any spaces. Also make sure your plugin is adding them exactly.

Secondly, I don’t know if you should be doing a while loop inside of a for loop. The for loop will be waiting for the while loop to end before it moves on. The problem is that the while loop will never end. This might be what is happening.

To fix this, you can wrap the while loop in a coroutine. This will create a new thread for each part so the while loops can run at the same time.
Alternatively there is also spawn() Sorry I couldn’t find much documentation with the developer hub, but it kinda does the same thing

1 Like

There are three issues with your code:

  • To apply the CFrame manipulation to the tagged part, reference the tagged part instead of script.Parent.
  • Execute each while statement in a new thread to prevent your code being blocked by one infinite while statement.
  • Avoid the conflicting variable identifiers. They are not causing any issues in this case, but may result in obscure bugs popping up later down the line.

Fixed code:

local collectionService = game:GetService("CollectionService")
local taggedParts = collectionService:GetTagged("Rotate")

local function handlePart(part)
    while true do
        part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(0, 0.02, 0)
        wait()
    end
end

for _, taggedPart in ipairs(taggedParts) do
    coroutine.wrap(handlePart)(taggedPart)
end
6 Likes