Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Could anyone help me cause I'm trying to rotate a part based on two values?

I have a group of parts and I want each one of them to rotate randomly.

local model = game.Workspace.Model
for name, child in pairs(model:GetChildren()) do -- Looping through all of the children
if child:IsA("MeshPart") then -- Checking if it's a part
        
        
        
        local container = {

            Vector3.new(0,180,0),
            Vector3.new(0,-180,0)

        }

        child.Orientation = container(math.random())
    end
end

2 answers

You have to use square brackets [] to access a table value by its index, also math.random() returns a random value from 0 (inclusive) to 1 (exclusive) but the only 2 indexes you need to access that table values are: 1 and 2. This should work:

local model = game.Workspace.Model
for name, child in pairs(model:GetChildren()) do -- Looping through all of the children
	if child:IsA("MeshPart") then -- Checking if it's a part
		local container = {
			Vector3.new(0,180,0),
			Vector3.new(0,-180,0)
		}
		child.Orientation = container[math.floor(math.random() + 0.5) + 1] -- Generate either 1 or 2
	end
end

Well, I assume that you want parts to rotate randomly. Well, if you want to rotate them instantly, you can do :

child.Orientation = container[math.random(1, #container)]

So, you might be thinking what we just did. Well, let's first talk inside the brackers, i.e., math.random(1, #container), here, 1 is a number and #container is the maximum number, which is the total number of elements in your array. So, math.random is taking random between 1 and largest number (2 in your case). And now, we have a random number, we just apply that number to that table, so we get our value.

NOTE : Don't use this method, if you are finding a random element in a Dictionary. In your case, it's an array, so it will work.

Also, if you want to animate the parts moving, you can use TweenService. Here's an example and what you should do :

local TweenService = game:GetService('TweenService')

local Part = script.Parent -- Sample Part

local tweenInfo = TweenInfo.new(
	2, -- Its the time (in sec) to complete the tween
	Enum.EasingStyle.Linear, -- Its the Style, I recommend you to leave it until you understand it fully
	Enum.EasingDirection.Out, -- Kind of same here
	0, -- If you change it with a negative number, the tween will perform indefinately. If you make it 2, it will perform 2 times
	false, -- Keep it false if you want the tween to rotate in one direction and not come back
	0 -- Delay time
)

local tween = TweenService:Create(Part, tweenInfo, {Orientation = Vector3.new(0, 180, 0)}) -- Loading our tween, first parameter is your Part, second parameter is your tweenInfo that we just created above, and third parameter is the property you want to change or tween

tween:Play() -- Plays our tween

This is how your whole code might look, with tween :

local model = game.Workspace.Model
local TweenService = game:GetService('TweenService')

for name, child in pairs(model:GetChildren()) do
		if child:IsA("MeshPart") then 
	
	        local container = {
	
	            Vector3.new(0,180,0),
	            Vector3.new(0,-180,0)
	
	        }
	
		local tweenInfo = TweenInfo.new(
			1, 
			Enum.EasingStyle.Linear, 
			Enum.EasingDirection.Out,
			0, 
			false,
			0 
		)
	
	    local Orientationn = container[math.random(1, #container)] -- Gets our value
	
		local tween = TweenService:Create(Part, tweenInfo, {Orientation = Orientationn}) -- Orientation is the property, and Orientationn is the random value selected
	
		-- You can add a wait(n) here, n must be a natural number, then the tween will player after n seconds
		tween:Play()
	end
end	

Note : That the tween will play one by one, as the parts will be inspected one by one. If you want all of it to happen all at once, I'll edit the code and give explanation to it.

Lemme know if it helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.