How can I turn this for loop into a while wait() loop?

Hi there, I have a simple script that generates grids using parts and a for loop. The issue is, is that, the parts load too quickly and require some sort of wait to prevent the game from crashing. I do not worry about if it’s meant to crash or not, I just want to turn this for loop in a while wait() loop. I have no attempt, other than the script itself as I do not know where to start.

local row = 256
local column = 144
local step = 1

for x = 1, row * step, step do
	for z = 1, column * step, step do
		local currPart = Instance.new("Part",workspace)
		currPart.Size = Vector3.new(step, step, step)
		currPart.Position = Vector3.new(x, 5, z)
		currPart.Anchored = true

	end
end

If you want to prevent it from being too quick, you can just put a wait() inside the for loop like so

local row = 256
local column = 144
local step = 1

for x = 1, row * step, step do
	for z = 1, column * step, step do
		local currPart = Instance.new("Part",workspace)
		currPart.Size = Vector3.new(step, step, step)
		currPart.Position = Vector3.new(x, 5, z)
		currPart.Anchored = true
	end
	wait()
end

How would I load the parts in lower batches instead?

local row = 256
local column = 144
local step = 1

for x = 1, row * step, step do
	for z = 1, column * step, step do
		local currPart = Instance.new("Part",workspace)
		currPart.Size = Vector3.new(step, step, step)
		currPart.Position = Vector3.new(x, 5, z)
		currPart.Anchored = true
		if z == (column * step)/2 then
			wait()
		end
	end
end
1 Like
local row = 256
local column = 144
local step = 1

for x = 1, row * step, step do
	for z = 1, column * step, step do
		local currPart = Instance.new("Part",workspace)
		currPart.Size = Vector3.new(step, step, step)
		currPart.Position = Vector3.new(x, 5, z)
		currPart.Anchored = true
		if z%100 == 0 then wait() end -- Loops before waiting (100)
	end
end
1 Like