Need some help with CFrame

I’m making a script that rotates models if they have a PrimaryPart.

I tried using this but it will just throw this error

local CF = workspace.Model:GetPrimaryPartCFrame()

CF = CF:ToOrientation(0,10,0)

workspace.Model:SetPrimaryPartCFrame(CF)
20:46:09.974 - Unable to cast double to CoordinateFrame
20:46:09.978 - Stack Begin
20:46:09.979 - Script 'local CF = workspace.Model:GetPrimaryPartCFrame()
20:46:09.979 - 
20:46:09.979 - CF = CF:ToOrientation(0,10,0)
20:46:09.980 - 
20:46:09.980 - workspace.Model:SetPrimaryPartCFrame(CF)', Line 5
20:46:09.980 - Stack End

I read through all the articles on the developer hub about CFrames but couldn’t find anything useful

How can I rotate the models with a PrimaryPart? (the whole thing)

2 Likes

You can use SetPrimaryPartCFrame and CFrame.Angles

number , number , number CFrame:ToOrientation ( )
Returns approximate angles that could be used to generate CFrame, if angles were applied in Z, X, Y order (Equivalent to toEulerAnglesYXZ)
This is what ToOrientation() does, it returns 3 numbers instead of a CFrame.

1 Like

CF:ToOrientation needs a CFrame argument to collapse the CFrame into a vector3. That’s not what you want to do if you are using SetPrimaryPartCFrame(). SetPrimaryPartCFrame needs a CFrame. I’m guessing you want to rotate your model by 10 degrees?

You can do this with CFrame.Angles(0, math.pi/180 * 10, 0) instead of your CF:ToOrientation method. When rotating CFrames, you can multiply them and it will apply the rotation. So you would instead have

CF = CF * CFrame.Angles(0, math.pi/180 * 10, 0)

The math.pi/180 is a scaling factor since I believe CFrame.Angles requires radian angle measurements, not degrees.

1 Like

This works but how would I write the code if I only want to rotate 1 axis instead of all 3 axis.

Each argument in CFrame.Angles(x, y, z) rotates around the axis you specify. Do you mean the local axis of the model?

If that’s the case then you need to use

CF = CF:ToWorldSpace(CFrame.Angles(0, math.pi/180 * 10, 0))

This would rotate along the vertical axis of the model.

(Also a warning before I forget: if you do this rotation a lot using SetPrimaryPartCFrame, it is prone to roundoff errors which leads to separate parts in your model moving away from their true location. “A lot” meaning > 1000 times on the same model. If you’re doing it sparingly then it isn’t an issue)

1 Like