Help Teleporting Player Character to Part's Localspace Z axis

Okay, I’ve been working on this for three days straight and now the CFrames and Vector3s are giving me a serious headache. Help would be appreciated.

local directpos = direct.Position
local rootpos = rootpart.Position
local chardist = (rootpos-directpos).Magnitude 
if chardist > 30  then return end
rootpos = Vector3.new(rootpos.X, rootpos.Y, directpos.Z)
rootpart.CFrame = CFrame.new(rootpos)

So this code doesn’t work right at all, and keep in mind this is the 40-somethingth iteration. I know the difference between CFrames and Vector3s but I’m bad at using them nonetheless

So in this code Direct is a part and rootpart is the HumanoidRootPart of the player.
So what I need is for this code to teleport the player’s character to the part “direct”. However, I only need the player teleported across one axis, the Z-axis, so that the player’s Z-axis becomes that of the part. Imagine the player just shifted rigidly along the Z axis to the part using the move tool. No additional rotation or change of any another axes.

Also–this needs to be to the part’s localspace, because I don’t know what orientation the part will be, it could be any rotation.

could you do something like:

char.HRP.Position.Z = direct.Position.Z

You can’t set one axis directly, but I’ll try doing this with CFrame math real quick

Now I’m at this

			local directpos = direct:GetPivot()
			local rootpos = rootpart.Position
			local charpiv = player.Character:GetPivot()
			local chardist = (rootpos-direct.Position).Magnitude 
			if chardist > 30  then return end
			player.Character:PivotTo(CFrame.new(charpiv.X, charpiv.Y, charpiv.Z))

Still doesn’t work right though. The rotation of the player isn’t preserved, and it doesn’t teleport to the part’s localspace.

Basically, I just need to be able to do two things:

Teleport a player to the part on one axis

For it to be teleported to the part’s localspace

It’s super easy to teleport a player across one axis, but unfortunately sometimes when the part is rotated, the part’s local Z axis is globally the X axis…

You can use CFrame.RightVector for this

local targetPosition = part.Position + part.CFrame.RightVector * 4
character:PivotTo(CFrame.new(targetPosition))