I am attempting to make a tram system that moves a tram along a table of nodes, which are parts on a track, using tween service. I can get the movement to the closest node working fine but then the node script recognizes that the closest node is the current one it is at causing the tram to stop forever. I know I need to make a if statement that detects if the current node is the closest one and then just skip it but I cant seem to figure out the implementation of it? If you can show me an updated script where it uses that technique I would greatly appreciate it. Thanks
local TS = game:GetService("TweenService")
local Engine = script.Parent:WaitForChild("Engine")
local Nodes = workspace:WaitForChild("Rails").RailNodes.Nodes
local NodeList = Nodes:GetChildren()
local closestNode = NodeList[1]
local lastClosestNode
function FindClosestNode()
for i,node in pairs(NodeList) do
if node.Name == "Node" then
if (node.Position - Engine.Position).Magnitude < (closestNode.Position - Engine.Position).Magnitude then
closestNode = node
end
end
end
return closestNode
end
function MoveForwardOneNode()
local Tween = TS:Create(Engine, TweenInfo.new(3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),
{CFrame = FindClosestNode().CFrame + Vector3.new(0,1.8,0)})
Tween:Play()
Tween.Completed:Wait()
return true
end
repeat wait() until MoveForwardOneNode()
MoveForwardOneNode()

3 comments
This helped a lot thank you am wondering to why we need the lowestnodistance when it never changes at all in the script
wait so I just plugged the code in and the tram still only goes one node
Yeah I apologize, I didn't change the lowestNodeDistance back, which I was supposed to do every time we're trying to find the closestNode, I have fixed it