Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago

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

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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.

0
I get a error stating that "fromEulerAnglesXYZ() is not a valid member" Orlando777 315 — 9y
3
fromEulerAnglesXYZ (alias Angles) is a CFrame constructor, toEulerAnglesXYZ (no alias) is the method to return the angles. adark 5487 — 9y
0
Sorry you're right, adark! I didn't bother to look it up and that was my mistake. Editing answer now. BlueTaslem 18071 — 9y
Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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.

Answer this question