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