Change only Y value with CFrame

Hey everyone, I am learning CFrame but don’t fully understand it.
I have the following script:

local newCFrame = CFrame.Angles(0, 0, 0)
	local offset = Vector3.new(0, 1,-5)

	clone:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame*CFrame.new(offset)) 
	clone:SetPrimaryPartCFrame(clone:GetPrimaryPartCFrame() * newCFrame)

I kind of understand what it does, but how would I change just the Y value, to (for example) 5? So that the Y value will always be five.

1 Like

To clarify, I meant, how would I set the primary part’s position to 5? (So the whole model goes with it, not just the primary part itself.)

there a few different was to do this but you might try something like this it will keep the original cframe rotation and then set it to the current x and z position but at the 5 mark on Y evertime

local HRPCFrame = character.HumanoidRootPart.CFrame  -- just get base cframe
local Rotation = HRPCFrame - HRPCFrame.Position  -- this removes the position from cframe leaving only a rotated cframe
local NewCFrame = Rotation + Vector3.new(HRPCFrame.X, 5, HRPCFrame.Z)  -- this adds back the new position set at Y 5
1 Like

Sorry, I still don’t get it, I just looked at a few pages about CFrames, and it’s still confusing to me…

Would it be possible to do something like modifying the Y value of the following line of code?

character.HumanoidRootPart.CFrame*CFrame.new(offset)) 

Actually, I understand it now. Are you saying that I should do something like this?

local newCFrame = CFrame.Angles(0, 0, 0)
	local offset = Vector3.new(0, 0,-8)
	local rootpartFrame = character.HumanoidRootPart.CFrame
	local rotation = rootpartFrame - rootpartFrame.Position
	local CompleteCFrame = rotation +  Vector3.new(rootpartFrame.X, 5, rootpartFrame.Z) 
	Tool.CraftingTable:SetPrimaryPartCFrame(CompleteCFrame*CFrame.new(offset)) 

Also, question: What does the line

Tool.CraftingTable:SetPrimaryPartCFrame(Tool.CraftingTable:GetPrimaryPartCFrame() * newCFrame)

do? Thanks so much :slight_smile: