Chunk Generation

So, I was tinkering around with chunk generation scripts found in the toolbox… and the result is that it does generate infinitely, but not in a gridlike manner. I do believe the first function “generateChunk” may do what I want, but I’m not sure how to manipulate it properly

math.randomseed(tick()/10)
local rows = 1
local columns = 1
print(rows,columns)
local chunksizeX = 20
local chunksizeZ = 20
function GenerateChunk(position)
	local chunk = chunks:GetChildren()[math.random(1,#chunks:GetChildren())]:Clone()
	chunk.Parent = workspace.Map.Generated
	chunk:SetPrimaryPartCFrame( CFrame.new(position))
	
end
function Generate(x,y,z)
	for i = 1,columns,1 do
for i = 1,rows,1 do
	x=x+chunksizeX

	GenerateChunk(Vector3.new(x,y,z))
end
z=z+chunksizeZ
x=0
	end
end

while true do
	for _,i in pairs(game.Players:GetPlayers()) do
		if i.Character ~= nil and i.Character:FindFirstChild("Head") then
			print("Generated Terrain for player " .. i.Name)
			GenerateChunk(Vector3.new(i.Character:FindFirstChild("Head").Position.X,1,i.Character:FindFirstChild("Head").Position.Z))
		end
	end
	game["Run Service"].Heartbeat:Wait()
end```

So, by what I am looking you want to use the “Generate” function. It seems like you give it a starting position. However, this position depends on the rows and columns. If you make something like “5” and “5” (25 in total) then it will generate in a grid.

It kind of looks right, but you can still see intersecting chunks when generated

Did you modify the chunk size variable? For instance if your chunk is 24x24 then you need to modify chunksizeX and chunksizeZ with 24

I have, both are set to 20 appropriately

Before anything, are those chunks super far away?

They appear to have been offset from my current location because of the changed variables

A question, why are you looping with “while”? I believe the offset is happening because you are generating the chunks every iteration. This means that when you move your player’s head it will move the chunks.

Another thing. Try to make a grid system instead of the actual position. This is what I mean:

You can see how there are 2 grids overlaying. One is player blue and the other one is red. What you want to do is make it so both of them use the same grid position (I guess you want to do that).

Imagine the purple dots are the center of each cell. You want to make so the chunks start generating the closest to the player using that purple dot. This will ensure that for those 2 players they generate in the same axis.

The maths is simple:

math.floor(x/chunksizeX + 0.5)*chunksizeX

This does what I showed above.

1 Like

An example of that formula is a grid that has 5x5 cells. By using the formula you can do this:

position = Vector3.new(math.floor(x/5 + 0.5)*5,0,math.floor(z/5 + 0.5)*5);

If my position is lets say 17 then the closest cell is 15:

  • 17/5 = 3.4
  • math.floor(3.4 + 0.5) = 3
  • 3*5 = 15

Games like minecraft for example use this feature since it will generate the chunk that you are in and not your local position.

1 Like

I think this formula is what I needed! I’ll play around with it some more, thank you

1 Like