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

Script causing server and or possibly client way too much lag. Another way to approach this?

This script i have spawns parts in a Vector3 dimension. But when i ran the script and check the Dev console, the script easily went up to activity of the script went up to at least 80% or more. Was wondering if there is any other way around this to help the server improve better. Suggestions or answers will help.

local SpawnCount = 0
for y = 1, 30 do
	for x = 1, 20 do
		for z = 1, 20 do
			GeneratePart(x, y, z)
			SpawnCount = SpawnCount + 1
			if SpawnCount >= 10 then
				wait()
				SpawnCount = 0
			end
		end
	end
end

2 answers

✓ Accepted answer

What you did was the easiest way of going through this problem, it is honestly completely fine, the only reason it is taking so long is because it is looping 12 thousand times. You can cut that down by a fraction by doing some simple math.

for y = 1, 15,2 do
    for x = 1, 10,2 do	
        for z = 1, 10,2 do
	    GeneratePart(x,y,z)
	    GeneratePart(x,y+1,z)
	    GeneratePart(x+1,y,z)
            GeneratePart(x,y,z+1)
	    GeneratePart(x+1,y,z+1)
	    GeneratePart(x,y+1,z+1)
	    GeneratePart(x+1,y+1,z)
	    GeneratePart(x+1,y+1,z+1)
        end
    end
end

That just cut your time in half by spawning multiple things in at a time, you can cut it down further by doing more like that if you wish.

17 comments

0

Also no reason to include that spawn count thing with the wait() in there as it is currently doing nothing, you aren't even printing the value, it could decrease the time it takes to run by quite a bit if you just removed the wait().

0

Tried your method, but it does out that it spawns that part and doesnt fill up the empty spaces before the script completes and plus it makes the script's activity go up even more.

0

activity will always be high on nested loops, the only difference is that this will take significantly less time. As for not filling in spaces, I'll see if I can come up with a better solution.

0

Updated it, it should work, but if it doesn't then I know my brain is fried for the day.

0

This wouldnt work as each layer will generate twice

0

No, it shouldn't generate each layer twice, it jumps to the next value by 2 so that it adds 2 layers at a time.

0

Nope, tested it

0

I just had it print each set of 3 coordinates and none were the same.

0

Might have tested the code again and saw u changed the increment by 2.. woops. It works but the problem is it leaves spaces in between.

0

Yeah I see that now, going to tweak it again, give me a min

0

Also, as for the stress it puts on the server/client, in cases of lots of parts being generated like this, you'll need to either find a balance between speed and efficiency or just go on the extremes of one or the other.

0

Ok it has been updated and I am almost certain now that it shouldn't produce gaps, and if all that GeneratePart is doing is cloning and moving things around the workspace, then it won't cause enough lag to do anything harmful to the client/server connection.

0

Well ermmm, its still creating gaps but less now.

1

Played around with the increment and the x,y,z, and now it works with less lag and no gaps :D tks

0

Lol it took us so many tries to get it done :) I also updated mine again and I still don't know if it works exactly, but it doesn't produce gaps anymore.

1

Wow now the code looks too messy lol. i Just made the X increment by 2 and made the x value to x+1.

0

Oh ok, yeah that would do what this is doing, but only for one variable. Your solution would be decreasing the time it takes by just a bit without causing any lag by generating too many parts at the same time. So you finally found the balance between speed and efficiency :D

You should never use wait() without a parameter, and do you really need to execute a loop 30 times to make another loop that loops 20 times that makes another loop to loop ANOTHER loop that loops for 20 times to generate a part?

9 comments

0

Umm that what i could think of to generate a part in Vector3 dimension... lol. Oh and plus if i were to use Wait(.05) or sth, it wouldnt just make the part to spawn delayed by 0.05 seconds. Correct me if im wrong.

0

Try creating just a single loop, and making the wait duration longer, also what are you using to create the part? Instance.new("Part", workspace)?

0

Actually I have the part already created and i just cloned it and set everything else before making the parent of it to the workspace

0

late reply but thats a good way of doing it, using the second parameter of instance.new creates big optimization problems if used frequently

0

So there isnt any other way to create parts like i have in a vector3 format without making the script's activity to reach over 80%?

0

Try lowering loops, and make the wait, wait more longer

0

You are wrong, you can always use wait(), thats the easiest way to wait the shortest possible time.

0

As for the loop within a loop within a loop, that's called a nested for loop, it is a very common practice and honestly one of the easiest ways to go about a problem like that, yes it will take time, but that is what is expected of that.

0

I dont care about the time taken but rather the stress it puts on the server/client

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