How to make a randomly rotating cube?

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?

Any guidance/help is appreciated, thanks!

1 Like

Sorry if I’m no help cause I’m a noob, but you could try messing around with cframes as you can easily achieve that by just messing with angles

I might be able to make one but it will take me a little bit of time

Just make a loop that changes its Orientation by adding one over and over.

1 Like

Here is the script

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)

Reply or mark as solution if this works

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.

1 Like

Hm, well it definitely works but gets stuck, and doesn’t move the player in the direction the cube is moving to make the player fall off.

Ok, thanks for the info i believe that i can work on that

I got this error:

Workspace.Cube.Script:20: attempt to perform arithmetic (div) on CFrame and number

I just stood on it while it was spinning, It did not stop.

When did it stop for you or is it such a low Orientacion change that you did not see it spinning?

You can change the change to a higher number or change the wait time to a lower number to be shure that it is working.

Ohh, my bad. Try this instead

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
1 Like

Can you tell me what is happening in this script. i cannot understand it.

It seems a little advanced to me, for what is happening.

Try using ball socket constraints. When a player stands on the cube it would rotate. It works when you attach it in the middle

i believe this is suppost to spin with a script for a constant rotation, unless i am wrong.

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

Yes i understand it well, is this a complete randomized number, or a somewhat chosen one?

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)

this does not work as intended, im going to work on it more.

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)
1 Like