I want to be able to make a large cube that rotates randomly in all directions while players try to stay on for as long as possible in my minigames game. However, unfortunately I’m not that advanced as a programmer and have no way as to how I will execute this. Does anyone have any ideas?
local Cube = script.Parent
local change = Vector3.new(1, 1, 1) -- how much you want to spin it
game.Players.PlayerAdded:Connect(function()
while true do
wait(0.5) -- how fast you want the changes to go
Cube.Orientation = Cube.Orientation + change
end
end)
I’m gonna assume that you want the cube to change direction during the round, so you could do
local reps = 50 -- amount of iterations in for loop
local t = 5 -- time it takes for the cube to rotate
local cube = script.Parent -- part goes here
while true do
local x, y, z = math.random(-1,1), math.random(-1,1), math.random(-1,1)
local rotation = CFrame.Angles(math.rad(x*360), math.rad(y*360), math.rad(z*360))
for i = 1, reps do
cube.CFrame *= (rotation/reps)
wait(t/reps)
end
end
I haven’t tested that, so just let me know if it doesn’t work. You could also use TweenService instead of a for loop.
local reps = 50 -- amount of iterations in for loop
local t = 5 -- time it takes for the cube to rotate
local cube = script.Parent -- part goes here
while true do
local x, y, z = math.random(-1,1), math.random(-1,1), math.random(-1,1)
local rotation = CFrame.Angles(math.rad(x*(360/reps)), math.rad(y*(360/reps)), math.rad(z*(360/reps)))
for i = 1, reps do
cube.CFrame *= rotation
wait(t/reps)
end
end
This generates a random number, either -360, 0, or 360, on the X Y and Z axis. Then, it adds the rotation in the for loop by using CFrame.Angles. In turn, this makes the cube rotate either -360, 0, or 360 degrees on all axes. Hope I explained that well
This is a bad practice because it’s only changing it manually and not by physics. It would just look robotic and unnatural. Also, the players on the cube probably won’t be able to move on it and causes lag problems.This applies to all other scripts above.
math.random(-1, 1) * 360 can only result in -360, 0, or 360. So I suppose it’s somewhat chosen? Since it does not include any of the numbers between -360 and 360. (except 0)
I just remade my script to be randomized more, Hope you like😀
local Cube = script.Parent
local max_rotation = 4 --max rotation
local Min_Rotaion = -4 --minimum rotation
game.Players.PlayerAdded:Connect(function()
while true do
wait(0.5) -- how fast you want the changes to go
local rotation = math.random(Min_Rotaion, max_rotation)
local change = Vector3.new(rotation, rotation, rotation)
Cube.Orientation = Cube.Orientation + change
end
end)