Terrain loading system crashes game

Im creating a block chunk based terrain generation system using perlin noise.

What happens is a function generates a dictionary containing the x and y position of the chunk, and the x, y, and z position of each block in the chunk.
Another function takes this dictionary and creates parts in the world in the form of the chunk data.

Problem is, whenever I run the function that converts the dictionary to physical blocks, roblox studio hangs until I tell it to force quit.

function loadChunk(chunkLoadData,chunksize,chunkheight)
	local chunkxPos = chunkLoadData["xpos"]
	local chunkzPos = chunkLoadData["zpos"]
	local blockData = chunkLoadData["storedchunkdata"]
	
	-- load blocks in chunk --
	
	local counter = 0
	
	for y = 0, chunkheight-1 do
		for x = 0, chunksize-1 do
			for z = 0, chunksize-1 do
				local blockType = blockData[counter]
				
				if blockType == 1  then
					
				else
					local blockPart = Instance.new("Part")
					blockPart.Parent = game.Workspace.chunks
					blockPart.CFrame = CFrame.new(Vector3.new(x+chunkxPos*16,y,z+chunkzPos)) -- this sets the position of the block to the block position relative to the chunk position
				end
				
				counter += 1
			end
		end
	end
	wait(0)
end

Note: chunkLoadData is the dictionary I explained before, chunksize is the width and length of the chunk, this is set to 16. The chunkheight is the height of the chunk, this is set to 64

I think the problem is that the script is trying to generate 16,384 blocks at the same time, But Im not sure how to fix this.

Any help would be appreciated

I’ve never seen wait(0) used before, just wait(). Could that be the issue?
I may be completely off.

If you want to make it wait, then put it after this

wait() and wait(0) are the same thing. However, they actually yield at least 29 milliseconds.

I was using the wait command as a attempted solution so it is not generating 16,384 blocks at the same time but rather 256, but That didnt work.

I tried what you suggested, It no longer crashes but rather is very VERY slow to generate.

Oops! I found the problem!

I wasnt anchoring the parts so the game was trying to calculate physics for 16,384 blocks at the same time.
my poor computer :frowning:

Its working now, Thanks for the help anyways!

1 Like