Hey so I have this system that basically caluclates the distance between waypoints and then finds out how long a tween has to be to remain at the constant speed for a train system im playing with, and I got that somewhat done but the issue now is how do I get other carriages to follow behind the train?
https://gyazo.com/1f8b98d9340254a30fcea44f162a40bc
Current code:
local TweenService = game:GetService("TweenService")
local Speed = 10 -- How many studs you want to go per second
local function calcDist(point1, point2)
return (point1 - point2).magnitude
end
local wp = 1
wait(3)
while game.Workspace.Waypoints:FindFirstChild(tostring(wp)) do
local Pos1 = script.Parent.PrimaryPart.CFrame
local Pos2 = game.Workspace.Waypoints:FindFirstChild(tostring(wp))
local Distance = calcDist(Pos1.Position, Pos2.Position)
print(Distance)
local Time = Distance/Speed
print(Time)
local TweenInf = TweenInfo.new(Time, Enum.EasingStyle.Linear)
game.TweenService:Create(script.Parent.PrimaryPart, TweenInf, {CFrame = Pos2.CFrame}):Play()
wait(Time) -- Wait for the tween to finish before moving to the next waypoint
wp = wp + 1
end