How to implement facing of part to CFrame that is being lerped, and moved by other lines?

Hello, I am trying to implement the right way of making PrimaryPart of train to face the right way, and then I want to lerp it. The problem is that in the code there is currently one lerp function, and I because I am new to CFrames I am not sure how to implement it. I am trying to implement it on lines 86-93

Here is the current code.

local trainoff = Vector3.new(1,9,0)
local offpos = trainoff + Vector3.new(0,0,script.OffZ.Value)
for i,v in pairs(script.Parent.Cars:GetChildren()) do
    local PrimaryPart = script.Parent.Cars["Car"..i]
    PrimaryPart:SetPrimaryPartCFrame(CFrame.new(workspace.Tracks.Start.PrimaryPart.Position + offpos))
    offpos = offpos + Vector3.new(0,0,script.Parent.Cars["Car"..i].Box.Size.Z)
end
--- Train Script Below
local RunService = game:GetService("RunService")

local Grinding = true

local function GetDist(p)
	return (p.Node1.WorldPosition - p.Node0.WorldPosition).Magnitude
end

local function GetNewPosition(p, a)
	return (p.Node1.WorldPosition - p.Node0.WorldPosition) * a + p.Node0.WorldPosition
end

local function GetNewNode(currentNode, dir)
   return currentNode.Part.NextNode.Value
end

local function GrindRail(Train, RailParts, CurrentPart)

	--AlignPosition.Attachment1.Position = AlignPosition.Attachment0.WorldPosition
	
	local CurrentNode = RailParts[CurrentPart]
	
	--alpha between Node0 and Node1
	local a = 0--dist2.Unit:Dot(dist1)/dist2.Magnitude
	--All variables should be found by now!

	
	
	--AlignPosition.Enabled = true
	
	local connection
	local CurrentRatio = GetDist(CurrentNode.Part)
	
	connection = RunService.Heartbeat:Connect(function(dt)
		a = a + dt*script.Speed.Value/CurrentRatio
		print(a)
		if a > 1 then
			repeat
				local newPart = GetNewNode(CurrentNode, 1)
				if newPart then
					CurrentNode = RailParts[newPart]
					local NewRatio = GetDist(newPart)
					a = a - 1
					a = a * NewRatio/CurrentRatio
					CurrentRatio = NewRatio
				else
					connection:Disconnect()
					break
				end
			until a <= 1
		elseif a < 0 then
			repeat
				local newPart = GetNewNode(CurrentNode, -1)
				if newPart then
					CurrentNode = RailParts[newPart]
					local NewRatio = GetDist(newPart)
					a = a + 1
					a = a * NewRatio/CurrentRatio
					CurrentRatio = NewRatio
				else
					connection:Disconnect()
					break
				end
			until a >= 0
		end
		
		if CurrentNode and Grinding then
			local dir = (CurrentNode.Part.Node1.WorldPosition - CurrentNode.Part.Node0.WorldPosition).Unit
			--[[Train.PrimaryPart.CFrame = Train.PrimaryPart.CFrame:Lerp(CFrame.new(
				GetNewPosition(CurrentNode.Part,a) + Vector3.new(0,10,0),
				GetNewPosition(CurrentNode.Part,a) + Vector3.new(0,10,0) + dir
			), 0.1)--]]
			for i,v in pairs(script.Parent.Cars:GetChildren()) do
		        local car = script.Parent.Cars["Car"..i]
		--        print(getdistancetonode(currentpart,dir,i))
		    --    print(i)
	            --print(ii.." "..i)
				local offpos = trainoff + Vector3.new(0,0,script.OffZ.Value)
	            

 -- I need to edit this block below

car:SetPrimaryPartCFrame(CFrame.new(
    	                car.PrimaryPart.Position
    	            ):Lerp(CFrame.new(
                    	GetNewPosition(CurrentNode.Part,a) + offpos + Vector3.new(-1,0,73.41*i),
                    	GetNewPosition(CurrentNode.Part,a) + offpos + dir + Vector3.new(-1,0,73.41*i)
    	            ), 0.1))
    				offpos = offpos + Vector3.new(0,0,73.41*i)
    			wait(0.001) 
    			end
    			wait(1/script.Speed.Value)
    			--AlignPosition.Attachment1.Position = GetNewPosition(CurrentNode.Part,a) + Vector3.new(0,3,0)
    		else
    			--AlignPosition.Enabled = false
    			wait(2)
    			Grinding = false
    		end
		
	end)
end

wait(1)
print("waiting for setup...")
wait(2.3)
print("ready!")

local train = script.Parent

local RailParts = {}
local RailModel = workspace.Tracks

for _, part in ipairs(RailModel:GetChildren()) do
	
	RailParts[part.PrimaryPart] = {
		Part = part.PrimaryPart
	}
end

GrindRail(train, RailParts, workspace.Tracks.Start.PrimaryPart)

Your help is much appreciated.

@sasial I think that you might be interested so I pinged you :slight_smile:

So you need the primary part to face what? A part? If yes I have a script. What do you mean for lerp?

I need primary part to face a node, and then lerp the movement and the facing to the node.

I am sorry for bad screenshot, I am using school PC at the moment.

EDIT: Nodes are the middle line of the rail. (Maroon color)

  1. lol good job for using pc
  2. you can use part. Cframe=cframe. New(part.position,faceme.position)
    You can set the height coordinate with the train one

Well, I know how I’d type it, but I am not sure how to implement it in that lerp, that is lerping the movement.

Setup the height so it wont face the node but the node with train height. BBS bye. What is a lerp?

Lerp is

CFrame:Lerp(-- frame goal, number alpha)

It’s a function that returns a CFrame interpolated between this CFrame and the goal by the fraction alpha.

Try setting your primarypart’s lookvector to the node’s lookvector possibly?

is there any way to make it as lerp afterwards? Because I am trying to lerp it, so it’s not instant, but “animated”.

So it moves cframe to a medium point right?
Between it and the goal.

Sort of. It uses an alpha argument that determines where the result CFrame is between the current CFrame and the goal. So if the alpha was 0.5, it’d end up directly in between the two CFrames.

Do you think that’s it’s possible to do it like this? I want to use it for curving the train.

I added a note, that will help you indicate what lines needs to be edited. ( I just need to add the code that will take care of the right facing)

part.CFrame.new(attribute 1, attribute 2) 

Because the script is really complex, and those attributes are lines of code, then I got confused in that script. And I am not really sure how to implement it, because I am new to using CFrames.

@jcnruad900 or is there any other way to make it curve and make it look like it’s moving?

Tween service maybe? I’m not sure of what you asked.

Yeah, I guess that it would work as well, I’ll give it a try.