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

Can't seem to make it respawn with a random orientation with my script?

Asked by 5 years ago

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.

0
I recommend you to use math.random(tick()); at the beginning of your script to have a new random generated seed for each clock tick. That way you do not expect always the same entrophy for the math.random. iDarkGames 483 — 5y

2 answers

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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
Ad
Log in to vote
0
Answered by
mc3334 649 Moderation Voter
5 years ago
Edited 5 years ago

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.

0
I would have put both answers as correct, since I was able to incorporate both answers to get exactly what I wanted, but I picked Lucy since I got the script to work as intended first, but your math.randomseed idea and GetRandomRot() is included for an improvement :) Thank you! sonicvsmaio4 4 — 5y

Answer this question