Chunk loading system w/ grid not working

Hey!

I am trying to make a chunk loading system by dividing the map into Region3 chunks, but it’s not going extremely well.
I have this code so far:

local xchunks = 10
local ychunks = 10

for i = 1,xchunks do
	for b = 1,ychunks do
		print("making region")
		local region = Region3.new(Vector3.new(512*(i-1), -100, 512 *(i-1)),Vector3.new((512*(i-1)) + 512, 100, (512 *(i-1)) + 512))
		for c,d in pairs(game.Workspace:FindPartsInRegion3(region)) do
			print(c.Name)
		end
	end
end

So as far as I know it works on the positive side, but I cannot find out how to make it work on the negative positions as well.
Nothing is printing as all of my bricks are on the negative side of the game.

I have tried putting the X Chunks variable to -10 by instead of using

for i = 1,xchunks do

I used

for i = xchunks,-xchunks,1 do

Anyone has any ideas on how to achieve this?

1 Like

You never actually use the b variable, so I’m guessing you just forgot to use that for the “Y” coordinates of the Region3. Here’s a slightly changed version. I also removed some unnecessary parentheses.

local region = Region3.new(
	Vector3.new( 512*(i-1),      -100, 512 *(b-1)),
	Vector3.new( 512*(i-1) + 512, 100, 512 *(b-1) + 512)
)

Putting it on multiple lines like this makes it a bit clearer what’s going on.

Making it more readable

This is just a matter of style, but I also like to get rid of magic numbers like so:

--Outside the nested loops
local chunkSize = 512
local cornerOffset = Vector3.new(chunkSize, 200, chunkSize)

--Inside the nested loops
local lowerCorner = Vector3.new( chunkSize*(i-1), -100, chunkSize*(b-1) )
local region = Region3.new(
	lowerCorner,
	lowerCorner + cornerOffset
)

You can make it even more readable by replacing the (i-1) with just i, and iterating from 0 to xchunks-1 instead.

1 Like

That looks really good. Thanks! I will test it right away! I will keep you updated!

Okay, that works, although it still doesn’t go in the negatives. Would I just re-loop it with a negative ChunkSize or?

I’m not sure what you mean by “in the negatives”.

Have you tried

for i = -xchunks, xchunks do
	for b = -ychunks ,ychunks do
		
	end
end

?

1 Like

Nope, I haven’t, I’ll do that now.

Also, with in the negatives I mean negative Vector3 values, because,most of my parts are in negative X or Z

This makes an infinite loop because you will never achieve a negative value -xchunks just because you are adding 1 instead of decreasing the value 'i'. Also, you might be careful if you are using decimal numbers (0.1,0.2 ,…) or natural numbers (1, 2, 3,…) so we don’t overpass the limit of a loop.


Natural number example:

local xchunks = 10
local ychunks = 10

for i = -xchunks, xchunks,1 do
	for b = -ychunks ,ychunks,1 do
		
	end
end

Decimal number, correct example :white_check_mark: :

local xchunks = 10
local ychunks = 10

for i = -xchunks, xchunks,0.1 do -- 9.9 + 0.1 = 10 // stops at 10
	for b = -ychunks ,ychunks,0.1 do
		
	end
end

Decimal number, incorrect example :x: :

local xchunks = 10
local ychunks = 10

for i = -xchunks, xchunks,0.3 do -- 9.9 + 0.3 = 10.2 //oops goes above limit 10, goes infinite.
	for b = -ychunks ,ychunks,0.3 do
		
	end
end
2 Likes