How to set an object with random orientation on run

Hello! I am Lows3D and I’m currently learning how to program in LUA. I started watching @Alvin_Blox beginner series.

I just did the Dice Video, but I went with my own format just to practice.
Here’s my code:

local D1 = game.Workspace:WaitForChild("Dice1")
local D2 = game.Workspace:WaitForChild("Dice2")
workspace.Gravity = 450 --// Sets Gravity
D1.Anchored = false --// Roll Dice 1
wait(.5)
D2.Anchored = false --// Roll Dice 2

I am wanting to add more variety to this dice game, as whenever I Run the script, I get the same results. I want to have a random result. And I need to set a random orientation for that.

If you guys help me, please don’t just send a few lines of code without explaining it. I’d like to learn from it, thanks!

1 Like
local D1 = workspace:WaitForChild("Dice1")
local D2 = workspace:WaitForChild("Dice2")


workspace.Gravity = 450 --// Sets Gravity

D1.Anchored = false --// Roll Dice 1

task.wait(5)

D2.Anchored = false --// Roll Dice 2

Updated Script

You’ll need to mess with the .Orientation property, and assign a Vector3.new() to it.

D1.Orientation = Vector3.new(0, 10, 0)

This would set the y orientation to 10 degrees. To make the orientation random, you’d want to set x, y, and z to random numbers so you can use math.random(min, max).

local randomNumber = math.random(1, 10)

This right here would give you a random number between 1 and 10. Make sure the first number it ALWAYS the smallest. Last step would be to combine what we just learned and do:

D1.Orientation = Vector3.new(math.random(1, 360), math.random(1, 360), math.random(1, 360))

There ya go!

1 Like

There’s a built in math library in LuaU that has a random function with this random function we can generate 3 random numbers to use as each Orientation Axis (X, Y, Z)

This random function has 2 parameters

Parameter 1 is the smallest random number it can pick
Parameter 2 is this biggest random number it can pick from

The random function then picks a random number somewhere in-between the first and second number (picks a number between parameter 1 parameter 2)

Since an object can only rotate 360 degrees we can set parameter 1 to “1” and parameter 2 to "360"

To do this we will make 3 variables called X Y Z then we will set each one equal to math.random(1, 360)

Then we will set the objects orientation property to a Vector3 with the 3 random numbers we made earlier (X,Y,Z)

E.G:

local D1 = game.Workspace.Dice1
local D2 = game.Workspace.Dice2

game.Workspace.Gravity = 450 

local X1,Y1,Z1 = math.random(1,360),math.random(1,360),math.random(1,360)
local X2,Y2,Z2 = math.random(1,360),math.random(1,360),math.random(1,360)

D1.Orientation = Vector3.new(X1,Y1,Z1)
D2.Orientation = Vector3.new(X2,Y2,Z2)

D1.Anchored = false --// Roll Dice 1
wait(.5)
D2.Anchored = false --// Roll Dice 2

Hope this helped reply if you don’t understand something ill be happy to help

2 Likes

Okay so for:

local X1,Y1,Z1 = math.random(1,360),math.random(1,360),math.random(1,360)

You wrote math.random(1, 360) 3 times to get a random rotation in the X, Y, and Z?

And if I am correct, X1…etc is for D1, and X2…etc is for D2?

Yes it is ________________________