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

For i, v in pairs loop in certain order?

I'm working on a tornado formation script, and I was wondering if there was a way to have a loop like this go through parts in a certain order:

local parts = script.Parent:GetChildren()

for i, part in pairs(parts) do
	if part:IsA("BasePart") then
		repeat
			part.Transparency = part.Transparency - 0.07
			wait(0.1)
		until
			part.Transparency <= 0.3
	end
end

What I tried to do was name the parts in a certain way so that they would be cycled through in alphabetical order, which is what I thought it would do. Is there a way I can have the loop cycle through parts in a certain order?

2 answers

The children are sorted by the order in which their Parent property was set to the object. So put the children in part in the order you want it to be in.

A fun thing to do so you don’t have to do repeat wait until, you can use tween to set the parts transparency to .3

It’s much easier to use once you learn it.

If you don't like the result of the default order. You can always use table.sort to get them in the right order before starting your loop:

local parts = script.Parent:GetChildren()

table.sort(parts, function(a, b)
	return a.Name < b.Name
end)

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