# Help with Model Orientation

Hello, I want to know how I can make a model to face 90, 180, 270, or 360 degrees, I want to make the Event choose a random rotation and spawn in facing that direction.

Here is a sample of the Events Code:

``````-- // Building
elseif event == 5 then
local Wedge = Events.Building.Wedge:Clone()
local position = Vector3.new(math.random(-214, 203), 8, math.random(-203, 214))
local cf = CFrame.new(position)
Wedge:PivotTo(cf)
Wedge.Parent = workspace.Generated
``````

How can I get the model to face a random direction?

Knowing whatâ€™s the error would be very very helpful. Just saying â€śHey I have an error how do I fix it?â€ť isnâ€™t enough to know whatâ€™s happening.

I canâ€™t exactly remember what I did to get the error, I donâ€™t remember what the error wasa in specific but I think it had to do with Orientation not being a member of the model

A model as no orientation, parts have. That may be confusing because in studio you can flip them with the orientation property but in script it doesnâ€™t exist. Use for example :PivotTo().

First of all, if you want to transform a `Vector3` orientation into a `CFrame`. You gotta use CFrame.Angles, implementing it is very easy, imagine this vector:

``````Vector3.new(90, 0, 45)
``````

Letâ€™s say we want to turn it into a CFrame.Angles, easy!:

``````CFrame.Angles(math.rad(90), 0, math.rad(45))
``````

Now if you want to generate a random rotation that can vary from 90, 180, 270 and 360. Itâ€™s easy! You can use the modulus (%) operator, which is like a division (/), but it instead returns the rest of the division.

``````-- generate a random number that can be: 90, 180, 270 or 360:
local randomNumber = math.random(0, 360)

local result = math.floor(randomNumber - randomNumber%90)
``````

`result` being the number youâ€™re searching for.

Now just hook this number up to `CFrame.Angles` inside your `PivotTo` function and youâ€™re done!

EDIT: I forgot to mention that if you want to add a `CFrame.Angles` to a `CFrame`, you gotta multiply it this way:

``````CFrame.new() * CFrame.Angles()
``````
1 Like

I tried this but I get an error saying â€śUnable to cast Vector3 to CoordinateFrameâ€ť

Here is the code I used

``````-- // Building
elseif event == 5 then
local Wedge = Events.Building.Wedge:Clone()
local position = CFrame.new(math.random(-214, 203), 8, math.random(-203, 214)) * CFrame.Angles(math.rad(90), 8, math.rad(45))
local cf = Vector3.new(position)
Wedge:PivotTo(cf)
Wedge.Parent = workspace.Generated
``````

Sorry for the late reply, but in the code you attached, youâ€™re trying to turn a `CFrame` into a `Vector3`. Not only that this method completely removes the CFrameâ€™s orientation but it also isnâ€™t compatible with `PivotTo`. Hereâ€™s a fixed version of your code:

``````-- // Building
elseif event == 5 then
local Wedge = Events.Building.Wedge:Clone()
local position = CFrame.new(math.random(-214, 203), 8, math.random(-203, 214)) * CFrame.Angles(math.rad(90), 8, math.rad(45))
Wedge:PivotTo(position)
Wedge.Parent = workspace.Generated
``````

Thatâ€™s the version that prevents the error from occuring. However as you were searching for a â€śrandom orientation only in 90, 180, 270 and 360â€ť this is its final code:

``````-- // Building
elseif event == 5 then
local Wedge = Events.Building.Wedge:Clone()

local RandomNumber = math.random(0, 360)
RandomNumber = math.floor(RandomNumber - RandomNumber%90)

local position = CFrame.new(math.random(-214, 203), 8, math.random(-203, 214)) * CFrame.Angles(0, math.rad(RandomNumber), 0)
Wedge:PivotTo(position)
Wedge.Parent = workspace.Generated
``````

If you have any doubt, please tell me

1 Like

Very sorry, for the mistake, Iâ€™ve been learning all this, it all works well now thank you for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.