New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

For i, v in pairs loop in certain order?

Asked by 8 days 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 8 days ago
Edited 8 days 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 8 days 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