How to make part follow a certain track

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    To make it so that a part follows this track and does not come off. (Attached Image)
    RobloxScreenShot20230123_095939453

  2. What is the issue? Include screenshots / videos if possible!
    I have no idea of how to achieve this yet.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried GnomeCode’s scripting, however that hasnt helped much.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

function move(x,y,z)
script.Parent.Position = script.Parent.Position + Vector3.new(x,y,z)
end

while wait(0.01) do
script.Parent.Position = script.Parent.Position + Vector3.new(1,0,0)
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

If the track is guaranteed to always be a perfect circle then you can do something like this:

local RunService = game:GetService("RunService")

local center -- vector3 in the center of the track
local radius -- number radius of the track around the center
local speed -- number of radians per second, you could do some pi math using the radius to use a stud/second speed instead

local currentAngle = 0
RunService.Heartbeat:Connect(function(delta) -- delta is how much time has passed since the last heartbeat
    script.Parent.CFrame =
        CFrame.new(center) -- start at the center
        * CFrame.Angles(0, currentAngle, 0) -- rotate around the center
        * CFrame.new(0, 0, radius) -- move forwards from the center, so that the train is on the tracks
        * CFrame.new(0, math.rad(90), 0) -- rotate 90 degrees, so that the train is pointing in the right direction

    currentAngle += speed * delta -- increase angle by speed * time
end)

You might have to move radius in CFrame.new(0, 0, radius) to a different axis if the train isn’t on the track. And you might have to change the 90 in math.rad(90) to -90 if the train is pointing in the wrong direction.

1 Like

Hi there, It’s not always going to be a perfect circle - this is purely for testing - how would this effect the code? Also, this code has not worked and my part has not moved.


You have to set the center, radius and speed variables to the right values.

But this code just calculates
image
the red dot using the blue angle (currentAngle), which it slowly increase to simulate movement.

To make a part follow any track you will need completely different and more complicated code. To start you should come up with a way to describe the shape of the track. With which you can create a function which returns a CFrame when you give it a number (distance on the track).

Put up boundaries around the track

If the track is made with models slightly oriented to create curves, I think an easy approach would be, moving the part iterating the whole track, getting the CFrame of a Root part of next track part, and moving the “moving part” to that CFrame coordinate, no matter how the track is the “moving part” would always be oriented following the track and “walking on it”

1 Like