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

Any other method for a nested for loop?

Asked by 4 years ago
Edited 4 years ago

This is from a previous question I asked about 2 weeks ago on how to make a generation script have less activity on the server/client. It does reduce the amount of the activity by at least 25-30% (Reduced from 75-80% to 60-40% [Estimate values/numbers]). However, sometimes the script takes forever to generate. Time ranging from 8.5 mins - 12 mins. This is too long of a wait. So was wondering if there is any other way to substitute a nested for loop and keep the script's activity on the server/client below at least 30% if it is even possible. This is the only way I could think of to generate a Vector3 (x, y, z) cube. Note:function GeneratePart calls a function to clone the part and calculates the position of it.

local SpawnCount = 0
for y = 1, 30 do
    for x = 1, 20, 2 do
        for z = 1, 20 do
            GeneratePart(x, y, z)
        GeneratePart(x + 1, y, z)
            SpawnCount = SpawnCount + 5
            if SpawnCount >= 10 then
                wait()
                SpawnCount = 0
            end
        end
    end
end
0
What is happening in GeneratePart() Overscores 381 — 4y
0
Just cloning a part in serverstorage and putting it in workspace whilst calculating the part's position. XviperIink 428 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The overhead of the nested for loop is going to be completely negligible compared to the time it takes to spawn parts and parent them into the workspace, so don't kill yourself trying to convert it to 1 loop. Those three loops with no functions inside will execute in microseconds, if that.

What's killing your performance is likely how you're spawning and parenting the parts inside GeneratePart. Nothing you show here accounts for this taking 10 minutes to run. There must be come real horrors happening in GeneratePart(), like using the 2-argument form of Instance.new or spawning huge, unanchored or overlapping parts, or something like that.

You might also be calling wait() more often than is needed. Try moving the wait() up to the x loop.

I have a triangle terrain demo place that spawns and orients 32,768 wedge parts, and the server only takes about 8 seconds to do this. So something is seriously amiss with spawning 6000 parts taking 8-12 minutes.

0
hmmmm. Well function generate part clones the part and I parent it to the workspace without using the second parameter. No overlapping parts. Blocks are 6,6,6 and are all anchored. Does it affect the rate if the position of the parts are like 50k,50k,50k x,y,z? Gonna try to remake it on a new game. See how wells it handles. XviperIink 428 — 4y
0
Ummm what??? Just tested it in a new place and have the generation set to x=20, y=35, z=20. Generated it and completed it 1.5mins.... XviperIink 428 — 4y
0
Are you testing in studio? If so, that could be partly why it's slow, just something to do with your PC. A Roblox game server on average runs Lua code about twice as fast as my PC, which is a decent machine. EmilyBendsSpace 1025 — 4y
Ad

Answer this question