Changing A Parts Z CFrame Not Working

Hello, I’m trying to change the z CFrame of a part using a script, but the part isn’t moving at all. The goal is to have a part move along its z-axis when a player moves along the part’s z axis, but to keep it’s x and y value the same.

Here’s a visualizer. If the player moves to along the x axis (it’s current left right) then don’t move the part, however if the player moves along the z axis (current forwards and backwards) move the part the same distance forwards/backwards that the player moves.

I’ve tried tweening it like this and get no errors, but the part doesn’t move at all.
local tween = TweenService:Create(camera, tweenInfo, {CFrame = camera.CFrame + Vector3.new(0, 0, HumanoidRootPart.Position)})

Thanks for reading!

Assuming you want to lock the part’s orientation, all you need to do is set the part’s position relative to the character’s position. Example:

local initialCFrame = part.CFrame
local relativePosition = initialCFrame:PointToObjectSpace(character.HumanoidRootPart.Position)
part.CFrame = initialCFrame - ((initialCFrame.LookVector) * relativePosition.Z)
1 Like