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

For i, v in pairs loop in certain order?

Asked by 3 years ago

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

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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.

Ad
Log in to vote
0
Answered by 3 years ago

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)

Answer this question