I have this script that makes a part follow nodes in number order using CFrame, and it works great except for one thing. Once it gets to the final node, instead of going back to node 1, it goes to some random node and THEN goes to node 1. The code is here:
Functions = {}
local Coaster=script.Parent
local PointsGroup=Coaster.Points
local Points=Coaster.Points:GetChildren()
local DifferenceBetweenPoints={0}
local CurrentTrackNumber={0}
local CurrentTrack={PointsGroup:FindFirstChild("1")}
local PreviousTrack={PointsGroup:FindFirstChild(#Points)}
local CurrentPosition={0}
Functions.MoveCar = function(Car)
local Train=script.Parent.Train
local Speed=script.Parent.Speed.Value
script.Parent.CurrentTrackNumber.Value=CurrentTrackNumber[1]
if Speed>0 then
DifferenceBetweenPoints[Car]=(CurrentTrack[Car].Position-PreviousTrack[Car].Position).magnitude
CurrentPosition[Car]=CurrentPosition[Car]+Speed
if CurrentPosition[Car]>=DifferenceBetweenPoints[Car] then
if CurrentTrackNumber[Car]<#Points then
CurrentTrackNumber[Car]=CurrentTrackNumber[Car]+1
PreviousTrack[Car]=CurrentTrack[Car]
CurrentTrack[Car]=PointsGroup:FindFirstChild(CurrentTrackNumber[Car])
CurrentPosition[Car]=CurrentPosition[Car]-DifferenceBetweenPoints[Car]
else
CurrentTrackNumber[Car]=1
CurrentTrack[Car]=Points[1]
PreviousTrack[Car]=Points[#Points]
end
if CurrentTrackNumber[Car]==#Points then
print("Currently on the last piece of track!")
CurrentTrackNumber[Car] =1
CurrentTrack[Car]=Points[1]
PreviousTrack[Car]=Points[#Points]
CurrentPosition[Car]=1
end
end
Train:FindFirstChild("Car"..Car):SetPrimaryPartCFrame(PreviousTrack[Car].CFrame:Lerp(CurrentTrack[Car].CFrame,(CurrentPosition[Car]/DifferenceBetweenPoints[Car])))
end
end
return Functions
I also published the game and made it uncopylocked so you can see for yourself how it works. Any help is appreciated! Stay safe and healthy.
Uncopylocked example game: https://www.roblox.com/games/4952063914/CFrame-Issue

6 comments
So this is for a part that follows invisible nodes using CFrame?
I added the best explanation i could
also this should work im using it in my game
use the scripts above
This isn't necessarily what I'm looking for, I'm just looking to edit the code I already have.
well im not that sure