Prevent CFrame on certain axis

im trying to cframe the character relative to another part, but only for the Y & Z axis instead of XYZ.

rootPart.CFrame = rail.CFrame:ToObjectSpace(CFrame.new(0,3.5,0))

this code puts the character above the part, but i also want to preserve the characters position on the X axis while doing so.

local offset = rail.CFrame:ToObjectSpace(CFrame.new(0,3.5,0))
local rootComponents = {rootPart.CFrame:GetComponents()}

rootPart.CFrame = CFrame.new(rootPart.Position.X, offset.Position.Y, offset.Position.Z, unpack(rootComponents, 4))

Give that a shot.

1 Like