How do I make this part slide through?

I am currently developing an Anti-Derailer script that will be similar to the RailConstraint in Roblox, but less complicated. The script aims to allow a train or part to move through the rail without any offset. However, I have encountered an issue that needs to be addressed.

The current script only works when the rail is straight and facing forward on the Z-axis. If I make the part move through the rail without offsetting the X-axis, it will go through. Unfortunately, this method doesn’t work with a rotated rail, even if the train or part is at the same angle as the rail.

Since my knowledge of scripting, especially with CFrame and Math, is limited, I have been trying various methods to solve this issue.

local part = script.Parent
local TweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)

local function onTouch(otherPart)
	if otherPart.Name == "Point" then
		game:GetService('TweenService'):Create(part, TweenInfo, {["CFrame"] = otherPart.CFrame * CFrame.new(0, 0, -1) + part.AssemblyLinearVelocity.Unit}):Play()
	end
end

part.Touched:Connect(onTouch)

I even used ChatGPT to solve the problem, but that didn’t work either.

Ive never heard of a RailConstraint

https://twitter.com/Bloxy_News/status/1448723274750316567

Oh sorry I don’t use twitter lol

So from looking at the code.

You are trying to take a Part and Tween it towards another part? And when the tween ends, it should either be in the same place as the “otherPart”, or I suppose, 1 stud in front of it?

The “+ part.AssemblyLinearVelocity.Unit” also looks a bit strange to me. But it is indeed always tricky wrapping your mind around CFrames at times.

If I’m correct, “otherPart.CFrame * CFrame.new(0, 0, -1)” is getting the CFrame of other part, and makes a copy of it, but pushes it in the “realtive forward direction”. So if otherPart is facing in some specific direction, the translation that you are doing here is pushing it 1 stud forward, relative to the parts orientation.

No, the part is supposed to only slide across the other part like a rail and the part cannot escape any where it can only go across the other part.