I'm trying to aim for the dice to spawn in the same place to roll down a staircase but have a different orientation each time so the result are much more random.
I tried using Vector3 to achieve the same effect, as in "script.parent.Orientation = (X,Y,Z)"
but to no effect on what faces were facing which direction, as they would always spawn in the same orientation (aka, if (and possibly before) the respawn script triggers, whatever face is facing me on top of the staircase will always be the exact orientation on subsequent respawns).
Here is my script inside a part called Dice inside a Model inside Workspace.
The script's Position line works, and it respawns it on top of the staircase after 10 seconds.
The CFrame orientation line however still seems to have a issue (without the SECOND Position line) where it spawns the dice seemingly in the middle of the baseplate.
With the SECOND Position line, it seems to have again no effect like the Vector3 equivalent.
local xPosition = math.random(-179, 179) local yPosition = math.random(-179, 179) local zPosition = math.random(-179, 179) local i = 1 while i < 10 do script.Parent.Position = Vector3.new(656.713, 548.883, 32.999) script.Parent.CFrame = CFrame.fromOrientation(xPosition,yPosition,zPosition) script.Parent.Position = Vector3.new(656.713, 548.883, 32.999) wait(10) print ("Loop could be established.") end
I am still new, and I apologize for any lack of things that should be obvious.
Thank you for reading.
your loop seems to go on forever, if you want that to happen, change your code to 'while true do' if you want it to go for ten times, change it to a for loop like in the code below: the reason the angles weren't changing were because your x y and z positions need to change every time you do the loop, not just at the beginning. I also changed the three lines of code you had to one because you can change the position by changing CFrame. i also changed the random values because rotation goes from 0 to 360 degrees
for i = 1, 10 do --create new random angle values each time local xPosition = math.random(0, 360) local yPosition = math.random(0, 360) local zPosition = math.random(0, 360) -- puts part at the top of the slope and multiplys the position by an angle to rotate dice randomly script.Parent.CFrame = CFrame.new(Vector3.new(656.713, 548.883, 32.999)) * CFrame.Angles(xPosition,yPosition,zPosition) wait(10) -- wait 10 sec to respawn it at a random rotation print ("Loop ".. i .. " finished") end
The code above is partially correct. Here is the simplified version.
math.randomseed(tick()) --Randomise math.random dice = script.Parent function GetRandomRot() return math.rad(math.random(0,360)) end while wait(10) do dice.CFrame = CFrame.new(Vector3.new(656.713, 548.883, 32.999))*CFrame.Angles(GetRandomRot(),GetRandomRot(),GetRandomRot()) end
Ok so breaking it down. The get random rotation function will return a random rotation in radians(becase CFrame.Angles takes radians not degrees). We use this function to call the 3 random numbers so we arent repetitive. Then in the while loop at the bottom, we wait 10 seconds then set the dices CFrame(Coordinate Frame) to the top of the stairs at a random rotation. With any questions, please feel free to respond to my comment.