How can I get the angles from CFrame.Angles?

Hi, I’m currently making a building system and I want to rotate the part 90 degrees when the player presses R. But when the player presses R it rotates the part until the player moves their mouse again because the rotation gets set back to normal. I believe this is because I’m using :SetPrimaryPartCFrame on 2 different lines, one of them rotating the part and the other one setting the rotation back to it’s original rotation. So to counter this I want to know how to get CFrame.Angles so I can apply it to the line that sets the rotation back to it’s original state. Here’s my code:

local PosX = math.floor(castRay.Position.X / GridSize) * GridSize + GhostItem.PrimaryPart.Size.X/2
local PosY = math.floor(castRay.Position.Y / GridSize) * GridSize + GhostItem.PrimaryPart.Size.Y/2
local PosZ = math.floor(castRay.Position.Z / GridSize) * GridSize + GhostItem.PrimaryPart.Size.Z/2
					
GhostItem:SetPrimaryPartCFrame(CFrame.new(PosX,PosY,PosZ))
			
UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.R then
		GhostItem:SetPrimaryPartCFrame(GhostItem.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(90), 0))
	end
end)
1 Like

To get the angles of a CFrame, you can use CFrame:ToEulerAnglesXYZ(). It gives the angles in radians.

There’s also another possible way to solve your problem, which is below.

local primaryCf = GhostItem:GetPrimaryPartCFrame()
GhostItem:SetPrimaryPartCFrame(primaryCf-primaryCf.Position+Vector3.new(posX, posY, posZ))
4 Likes

How would I replicate the rotation of the part over to the server? Because this is a client part I pass the position of the part over to the server to make it place down at the position of the part on the client. So how would I also send over the rotation of the part?

2 Likes

You could send the LookVector of the part’s CFrame. Then, on the server, to create the CFrame, you use the position and the LookVector like this.

local cf = CFrame.new(pos, pos+lookVector)
4 Likes