Would doing this be detrimental?

This is my code setup:

for i,v1 in pairs(--blah blah) do
    -- Stuff
    for i,v2 in pairs(--blah blah) do
        -- Stuff
        for i,v3 in pairs(--blah blah) do
            -- Stuff
            for i,v4 in pairs(--blah blah) do
                -- Stuff
                for i,v5 in pairs(--blah blah) do
                    -- Stuff
                end
            end
        end
    end
end

Would doing this be bad IN ANY WAY besides horrible code setup? Examples are intense lag, etc.

EDIT: This is a button handler for all the buttons in a tycoon. The -- Stuff would be making new local variables that would go in place of the --blah blah in pairs(--blah blah:GetChildren()).

The for loops alone won’t cause lag at all. It’s all about the “stuff” inside of those loops. The for loop just handles iteration, but doesn’t really cost the script much time.

However, that is quite a immense amount of loops, so I would think it would be rare when you would need that many. It’s best not to stack as much as possible for cleanliness reasons. There is normally a simpler option.

1 Like

For starters, the first loop defines “v” so all the other loops cannot use that to define the value in the table.

Additionally, you wouldn’t have to put loops within each other unless you plan an iterating through a table that was returned by another loop. For example:

for i, v in pairs(tableName) do
    for a, b in pairs(v) do
        --This would return the values within the table that the first loop iterated
    end
end

Aside from that, you should be running the loops separately. Eg.

for i, v in pairs(tableName) do
    --event
end
for a, b in pairs(tableName) do
    --event
end
1 Like

@ExcessEnergy
Thanks, that’s what I thought. I updated the post btw. This is a button handler for a tycoon and the stuff would be new variables obtained fron the for loop that would be used in the next for loop.


@CreationNation1
Don’t worry, in the actual script they are all different. This is just the rough draft of how my code is set up.

:watermelon: Hiya!

Yes! It would! Every table you create takes up memory and space, and the script has to process and spend more time going through each table. Especially when you have complex functions like :GetDescendants(). You can really look into how your scriptins are causing lag by doing the following:

Go to script performance, check the memory each script takes up. A way to increase code functionality, keep lag minimal to none, would be using events or modules! You can reference to them via: Events | Documentation - Roblox Creator Hub & ModuleScript | Documentation - Roblox Creator Hub!

1 Like

No, no, noo, no.

Please don’t do that. Just don’t. If you’re using multiple for loops, I would suggest not putting them inside of each other. I don’t understand why you need that much unless you’re checking something for each loop beforehand. I don’t believe it has any significant impacts, though.

1 Like

If you really must use that many loops, then go ahead. Just be carful you aren’t using more than you need, and that you aren’t performing the looping to fast while calling functions or doing tasks that take up a lot of CPU.

Using as many loops as you mentioned in your OP still seems a bit excessive though. Especially for readability reasons I would try to veer away from that many.

1 Like

Agreed. It takes up a LOT of memory, space, game functionality.

1 Like

Ok, thanks everyone! I’ll try to get rid of a few loops that aren’t necessary and turn them into if then end statements. They’re searching for a certain name in each loop and finding the model with it. I could just do :FindFirstChild I guess.

Thanks! :slight_smile: