I am currently developing a nodes based train system for my game. However, I am quite stuck on how to make multiple cars for a train. Since there is a bit of wait time between each execution, making them as two separate model will make then out of sync and crash into each other. Is there a better (and more efficient) way to do it? Thanks. I have linked my code for one car down below for reference.
local Node = 6
local TweenService = game:GetService("TweenService")
local speed = script.Parent.Parent.Speed
local part = script.Parent.PrimaryPart
local activeTween
function GetNextNode()
Node = Node + 1
return game.Workspace.Nodes:FindFirstChild("Node"..tostring(Node))
end
local function MoveToNextNode()
local goalNode = game.Workspace.Nodes:FindFirstChild("Node"..tostring(Node))
-- Function to start a tween toward the current goal node
local function StartTween()
if goalNode and speed.Value > 0 then
local distance = (goalNode.Position - part.Position).Magnitude
local tweenTime = distance / speed.Value
-- Cancel any active tween
if activeTween then
activeTween:Cancel()
end
-- Create and play new tween
activeTween = TweenService:Create(part, TweenInfo.new(tweenTime, Enum.EasingStyle.Linear), {CFrame = goalNode.CFrame})
activeTween:Play()
-- When tween completes, check if part reached the node
activeTween.Completed:Connect(function()
if (part.Position - goalNode.Position).Magnitude < 0.1 then
goalNode = GetNextNode()
StartTween() -- Move to the next node
end
end)
end
end
-- Listen for speed changes to restart the tween
speed:GetPropertyChangedSignal("Value"):Connect(function()
StartTween()
end)
-- Start initial tween
StartTween()
end
MoveToNextNode()
Hi, if you want to make multiple cars, you can do what i just did. My system works slightly differently as it doesn’t rely on tweens but relies entirely on CFrame. You can CFrame extra cars simply by repeating a function and its target.
My movement code basically uses target, which is the PrimaryF or PrimaryB part, connected via a base. I simply do
Move(primaryF1, dT)
Move(primaryF2,dT)
then i do a code to link up the base with both of these,
basically stuff like that. You don’t have to repeat the entire script over and over, you just have to repeat the parts you are tweening for a second car. There is a risk of disconnecting cars which unfortunately cannot be solved, but it’s very unlikely. This way i dont have to create a CFrame movement script for each car.
NOW, onto what you want, i suggest this
local Node = 6
local TweenService = game:GetService("TweenService")
local speed = script.Parent.Parent.Speed
local part = script.Parent.PrimaryPart
local part2 -- Basically any second car primaryPart, follow the same steps for the third car and so on and forth.
local activeTween
function GetNextNode()
Node = Node + 1
return game.Workspace.Nodes:FindFirstChild("Node"..tostring(Node))
end
local function MoveToNextNode()
local goalNode = game.Workspace.Nodes:FindFirstChild("Node"..tostring(Node))
-- Function to start a tween toward the current goal node
local function StartTween(Target)
if goalNode and speed.Value > 0 then
local distance = (goalNode.Position - Target.Position).Magnitude
local tweenTime = distance / speed.Value
-- Cancel any active tween
if activeTween then
activeTween:Cancel()
end
-- Create and play new tween
activeTween = TweenService:Create(Target, TweenInfo.new(tweenTime, Enum.EasingStyle.Linear), {CFrame = goalNode.CFrame})
activeTween:Play()
-- When tween completes, check if part reached the node
activeTween.Completed:Connect(function()
if (Target.Position - goalNode.Position).Magnitude < 0.1 then
goalNode = GetNextNode()
StartTween(part) -- Move to the next node
StartTween(part2) -- This may link up both cars, and stuff may be out of alignment so it will seem like both cars are "welded" to each other as one single car, but i am afraid im not able to do anything as im not experienced with this system
end
end)
end
end
-- Listen for speed changes to restart the tween
speed:GetPropertyChangedSignal("Value"):Connect(function()
StartTween(part)
StartTween(part2)
end)
-- Start initial tween
StartTween(part)
StartTween(part2)
end
MoveToNextNode()
I apologize if there may be any errors as i’m not as familiar with this system as the usual CFrame system which doesn’t use tweens.