How do I delete a bunch of blocks the fastest way possible without any lag?

I have a block terrain generator that generates something that looks like this:


I’m trying to make a loop where it keeps on generating new terrain every few minutes, which requires me to clear all the blocks and then generate new ones. I am trying to find a fast way to delete the blocks without any lag. This is probably the fastest I could get:

for i,v in pairs(Folder:GetChildren()) do
	v:Destroy()
	RunService.Heartbeat:Wait()
end

I tried doing this as well:

for i,v in pairs(Folder:GetChildren()) do
	v:Destroy()
    -- no wait at all
end

Doing this without a wait makes me lose a ton of frames, which is something I don’t want.

Here is a video on how long it takes for the blocks to clear out with the wait:

Does anyone know any faster ways to clear out all the blocks while reducing lag? I’ve been trying to look for different solutions but never found anything useful.

2 Likes

Have you tried deleting the whole folder object directly?

2 Likes

It works but it feels just as laggy as doing the for loop without waiting at all.

You can wait every n blocks like this:

for i,v in pairs(Folder:GetChildren()) do
	v:Destroy()
	if i % 100 == 0 then --100 for example
		RunService.Heartbeat:Wait()
	end
end

You could also simply reduce the number of blocks if you intend to make a mining game for example, every time you mine a block you ensure there are 5 new blocks on the sides.

4 Likes

Thank you! Just what I wanted.

I know that your question was answered, but can I also suggest not deleting parts at all?

Instancing/destroying has a lot of overhead. If you can reuse the parts from one terrain generation for the following generation, you can do things much quicker!

You could probably even use a premade module like this to cache parts for you, or you could implement it yourself.

1 Like