Having trouble with node-based train system

Recently I have developed a node-based train system similar to games like the Wild West or Jailbreak. My primary issue is attempting to add multiple passenger cars to my system. I have tried multiple methods and none have seemed to work for me. The goal is for the cars to maintain an equal distance apart, all while traveling at the same speed with an orientation relative to the node below it.

What ends up happening is this: https://gyazo.com/10c66b2f17102fef49184855d7ddea29.mp4
I have viewed other topics an none had any helpful information that appeared relative to the problem i’m facing.

My current code:

local TrainModel = script.Parent
local Frame = TrainModel:WaitForChild("Frame")
local PrimaryPart = Frame:WaitForChild("Main")

local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local TrainHandlerModule = require(ServerStorage.TrainModule)
local Train = TrainHandlerModule.Init(TrainModel)

local StopLength = 5
local TrackNodes = {}

local Transitions = {
    ['Dusty Rose'] = 0, -- Red
    ['Red flip/flop'] = 10, -- Orange
    ['Fawn brown'] = 20, -- Yellow
    ['Shamrock'] = 20, -- Green
}

for _, Nodes in pairs(workspace.TrainNodes:GetChildren()) do
    table.insert(TrackNodes, Nodes)
end

while true do -- i kinda hate this 

    for _, Node in pairs(TrackNodes) do -- and this 


        local TweenGoal = {}
        local Transition = Transitions[tostring(Node.BrickColor)]

        local Distance = (workspace.Trolly.PrimaryPart.Position - Node.Position).magnitude
        local TrainSpeed = Train:CalculateSpeed(Distance, Transition)

        if Transition == 0 then
            Train:ToggleDoors()
            TrainSpeed = Train:CalculateSpeed(Distance, Transitions['Red flip/flop'])
        end
        
        --Metamethod later
        TweenGoal.CFrame = Node.CFrame + Vector3.new(0, 0, 0) 
        local TrainTween = TweenService:Create(PrimaryPart, TweenInfo.new(TrainSpeed, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), TweenGoal)
        TrainTween:Play()
        TrainTween.Completed:Wait()
        
        -- MAKE CLIENT SIDED LATER
    end
end
3 Likes

I don’t know if it will help you, and if you solved it, it was a long time ago, but I made my train by doing all things separately, and I just timed with Wait () when they could run behind them.
It should be added that my train never stops on the map. If he stopped, the wagons would be inside.
so I hope it helps you at least a little. I see no one answered you.

1 Like

You should use another way to make this, like make them attached between themselves and follow the trail as a normal one, try study AlignPosition | Roblox Creator Documentation and AlignOrientation | Roblox Creator Documentation or just WeldConstraint | Roblox Creator Documentation