Chunk loading and unloading

So i just made a chunk-loading system. When it load/unload chunks the code is very repetitive. Is there any way to make this shorter with a for loop or something?

Thanks

local RepStore = game:GetService("ReplicatedStorage")
local children = workspace:WaitForChild("Chunks"):GetChildren()

local player = game.Players.LocalPlayer

for i = 1, #children do
	children[i].Name = math.floor(children[i].Position.x/100 + .5) .. ", " .. math.floor(children[i].Position.z/100 + .5)
	children[i].Parent = RepStore.Chunks
end

wait(1)

while wait(1) do
	local chords = player.Character.HumanoidRootPart.Position
	local x = math.floor(chords.x/100 + 0.5)
	local y = math.floor(chords.y/100 + 0.5)
	print(x, y)
	
	if RepStore.Chunks:FindFirstChild(x .. ", " .. y) then
		RepStore.Chunks:FindFirstChild(x .. ", " .. y).Parent = workspace.Chunks
	end
	
	if RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y) then
		RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y).Parent = workspace.Chunks
	end
	
	if RepStore.Chunks:FindFirstChild(x .. ", " .. y+1) then
		RepStore.Chunks:FindFirstChild(x .. ", " .. y+1).Parent = workspace.Chunks
	end
	
	if RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y+1) then
		RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y+1).Parent = workspace.Chunks
	end
	
	if RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y) then
		RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y).Parent = workspace.Chunks
	end
	
	if RepStore.Chunks:FindFirstChild(x .. ", " .. y-1) then
		RepStore.Chunks:FindFirstChild(x .. ", " .. y-1).Parent = workspace.Chunks
	end
	
	if RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y-1) then
		RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y-1).Parent = workspace.Chunks
	end
	
	if RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y+1) then
		RepStore.Chunks:FindFirstChild(x-1 .. ", " .. y+1).Parent = workspace.Chunks
	end
	
	if RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y-1) then
		RepStore.Chunks:FindFirstChild(x+1 .. ", " .. y-1).Parent = workspace.Chunks
	end
	
	
	
	if workspace.Chunks:FindFirstChild(x+2 .. ", " .. y+2) then
		workspace.Chunks:FindFirstChild(x+2 .. ", " .. y+2).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x+2 .. ", " .. y+1) then
		workspace.Chunks:FindFirstChild(x+2 .. ", " .. y+1).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x+2 .. ", " .. y) then
		workspace.Chunks:FindFirstChild(x+2 .. ", " .. y).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x+2 .. ", " .. y-1) then
		workspace.Chunks:FindFirstChild(x+2 .. ", " .. y-1).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x+2 .. ", " .. y-2) then
		workspace.Chunks:FindFirstChild(x+2 .. ", " .. y-2).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x+1 .. ", " .. y-2) then
		workspace.Chunks:FindFirstChild(x+1 .. ", " .. y-2).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x .. ", " .. y-2) then
		workspace.Chunks:FindFirstChild(x .. ", " .. y-2).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x-1 .. ", " .. y-2) then
		workspace.Chunks:FindFirstChild(x-1 .. ", " .. y-2).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x-2 .. ", " .. y-2) then
		workspace.Chunks:FindFirstChild(x-2 .. ", " .. y-2).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x-2 .. ", " .. y-1) then
		workspace.Chunks:FindFirstChild(x-2 .. ", " .. y-1).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x-2 .. ", " .. y) then
		workspace.Chunks:FindFirstChild(x-2 .. ", " .. y).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x-2 .. ", " .. y+1) then
		workspace.Chunks:FindFirstChild(x-2 .. ", " .. y+1).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x-2 .. ", " .. y+2) then
		workspace.Chunks:FindFirstChild(x-2 .. ", " .. y+2).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x-1 .. ", " .. y+2) then
		workspace.Chunks:FindFirstChild(x-1 .. ", " .. y+2).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x .. ", " .. y+2) then
		workspace.Chunks:FindFirstChild(x .. ", " .. y+2).Parent = RepStore.Chunks
	end
	
	if workspace.Chunks:FindFirstChild(x+1 .. ", " .. y+2) then
		workspace.Chunks:FindFirstChild(x+1 .. ", " .. y+2).Parent = RepStore.Chunks
	end
	
end
3 Likes

try looking at okeanskiy tutorials on chunks

The way I’d suggest to do it would be with for loops of i^2. The first loop would be for offsetting the x direction, and then the second loop would be for the y direction.

Since I’m on mobile I’ll write some sudo code.

local range = 2

for xOffset = -range, range do
    for yOffset = -range, range do
        createCunk(x + xOffset, y + yOffset) --make the actual code
    end
end

That should create a square of loaded chunks around the player with a worth and height of the range value (can be changed easily)
However the loading will start from the end of the square and move done horizontally, which is one downside of this method

1 Like

One way is to make your code more data-oriented, e.g.:



local offsets = {
	Vector2.new(0, 0),
	Vector2.new(1, 0),
	Vector2.new(0, 1),
	Vector2.new(1, 1),
	Vector2.new(-1, 0),
	Vector2.new(0, -1),
	Vector2.new(-1, -1),
	Vector2.new(-1, 1),
	Vector2.new(1, -1),
	Vector2.new(2, 2),
	Vector2.new(2, 1),
	Vector2.new(2, 0),
	Vector2.new(2, -1),
	Vector2.new(2, -2),
	Vector2.new(1, -2),
	Vector2.new(0, -2),
	Vector2.new(-1, -2),
	Vector2.new(-2, -2),
	Vector2.new(-2, -1),
	Vector2.new(-2, 0),
	Vector2.new(-2, 1),
	Vector2.new(-2, 2),
	Vector2.new(-1, 2),
	Vector2.new(0, 2),
	Vector2.new(1, 2),
}

while wait(1) do
	local playerPos = player.Character.HumanoidRootPart.Position
	local x = math.floor(playerPos.X/100 + 0.5)
	local y = math.floor(playerPos.X/100 + 0.5)
	print(x, y)
	
	for _, offset in pairs(offsets) do
		local chunk = RepStore.Chunks:FindFirstChild(x + offset.X .. ", " .. y + offset.Y)
		if chunk then
			chunk.Parent = workspace.Chunks
		end
	end		
end

If there’s some mathematical relationship between the different offsets, you can generate them on-the-fly, like so:

local chunkGenRadius = 2

while wait(1) do
	local playerPos = player.Character.HumanoidRootPart.Position
	local playerChunkX = math.floor(playerPos.X/100 + 0.5)
	local playerChunkY = math.floor(playerPos.X/100 + 0.5)
	print(x, y)

	for chunkOffsetX = -chunkGenRadius, chunkGenRadius do
		for chunkOffsetY = -chunkGenRadius, chunkGenRadius do
			local chunk = RepStore.Chunks:FindFirstChild(playerChunkX + chunkOffsetX .. ", " .. playerChunkY + chunkOffsetY)
			if chunk then
				chunk.Parent = workspace.Chunks
			end
		end
	end	
end
1 Like