Creating a platform that moves from one point to another continuously with CollectionService?

Hi , I’m new to scripting with Roblox’s code language and I’m trying to create a platform that moves a set distance one way, then moves back the other. I recently learnt about CollectionService and how it’s a much better system to use than just placing a script into a part and having it work that way.

I have used the tag plugin in order to give the part I want to move a tag to be able to use CollectionService, and have tried to create a script that allows all of the parts with that tag to move, but I can’t seem to get it to work. Here is my script:

Script

I’ve tried my hardest to stick to other instances of CollectionService on parts that I’ve seen and it still won’t work. Any tips to help me would be greatly appreciated ! ^^

Obviously you would need to run some type of infinite loop and make sure they run asynchronously. In your case, a simple task.spawn and a while true do will suffice.

for _, taggedPart in CollectionService:GetTagged("someTag") do
    -- run on a separate thread
    task.spawn(function()
        -- infinite loop
        while true do
            -- repeating code
        end
    end)
end
1 Like

You want to move each and every moving part forever right? What you are doing right now will move every part one by one and it won’t run forever. To fix this, you want to spawn the set of for loops in a while true do loop. Here’s how to do it:

local distance = 16
for _,TaggedPart in pairs(TaggedParts) do
   task.spawn(function()
      while task.wait(2) do
         for i = 0,distance do
            script.Parent.CFrame += Vector3.new(2,0,0)
            task.wait()
         end
         task.wait(2)
         for i = 0,distance do
            script.Parent.CFrame += Vector3.new(-2,0,0)
            task.wait()
         end
      end
   end)
end
1 Like

Uh, why?
Make 1 Part anchored, Weld one or more unanchored Parts to it, and tween the anchored one back and forth.

If you just CFrame it then players will slide off because CFraming isn’t a physical movement, but tweening the Anchored part with the other welded Parts welded to it makes it a physical movement that players can interact with.

1 Like

I should have said that this was for a killbrick, instead of a platform. Thank you for the tip for platforms though! I was going to start learning how to do the same thing with those later on ^^

You could do the exact same thing with your kill bricks.
If you have them moving different directions then you may need multiple tweens, but they are really simple to use, and you wouldn’t have to loop through all your tagged Parts.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.