Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I correct this CFrame If statement? [Not Solved]

I want to make an if statement using CFrame angles to say that if the angle for Y is over math.rad(30) then make it equal to math.rad(30).

if part.CFrame.fromEulerAnglesXYZ.Y > math.rad(30) then
	part.CFrame.Y = math.rad(30)
end

EDIT: I'm trying to rotate a Motor6D.C0 I tried what you all said, but apparently when I print the CFrame I see not rotation value difference when the thing is rotating before my eyes .-.

2 answers

toEulerAngles is a method, so we use : instead of . for it. (Use toEulerAngles not fromEulerAngles as adark pointed out!)

Unlike most functions, it returns multiple values, so there actually isn't a clean way to do it inline in the if statement.

local rx, ry, rz = part.CFrame:toEulerAnglesXYZ()
if ry > math.rad(30) then

Alternatively you could look at the Rotation property of Parts that is measured in degrees and does this math for you.

3 comments

0

I get a error stating that "fromEulerAnglesXYZ() is not a valid member"

3

fromEulerAnglesXYZ (alias Angles) is a CFrame constructor, toEulerAnglesXYZ (no alias) is the method to return the angles.

0

Sorry you're right, adark! I didn't bother to look it up and that was my mistake. Editing answer now.

Your problem is that the X, Y, and Z properties of CFrame are readonly. This means that they cannot be set, only accessed as a number. This means that

workspace.Part.CFrame.X = 5

will not work, while

local x = workspace.Part.CFrame.X
print(x)

will work. Again, you cannot change the XYZ properties of CFrame, but you can read them.

We can accomplish what you want, however, by doing the following:

part.CFrame = part.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(30), 0)

Here, we are taking the part's CFrame, and setting it to itself. We are then multiplying (although it seems like we are adding, more on that here) it by 0, math.rad(30), 0. Thus the X and Z does not change, only the Y is affected.

On a side note, although CFrame.fromEulerAnglesXYZ works fine, it is more readable and quicker to type to use CFrame.Angles.

Hope I helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.