Need help with my chunk load and unload system

Its been a while that im trying to make a chunk load and unload system for my infinite terrain script. I managed to make the loading part of the script (it load a new terrain chunk depending on the render distance) but i really dont know how to implement the unloading functionality as the code does not use parts but it uses Smooth Terrain instead, and plus i dont know how to make it unload parts either as my code needs as weel to load / unload trees and vegetation. I know it sounds confusing so here the full code.



local baseHeight 		= 6				-- The main height factor for the terrain.
local chunkScale 		= 3 				-- The grid scale for terrain generation. Should be kept relatively low if used in real-time.
local renderDist 		= 120/4 			-- The length/width of chunks in voxels that should be around the player at all times
local xScale 			= 90/4				-- How much we should strech the X scale of the generation noise
local zScale 			= 90/4				-- How much we should strech the Z scale of the generation noise
local generationSeed	= math.random() 	-- Seed for determining the main height map of the terrain.
 
------------------------------------------------------------------------------------------------------------------------------------------------
 
local chunks = {}
 
function chunkExists(chunkX,chunkZ)
	if not chunks[chunkX] then
		chunks[chunkX] = {}
	end
	return chunks[chunkX][chunkZ]
end


local function addVegetation(x, y , z)
	if math.random(1,10) == 5 then
		
		local meshDirectory = game.ReplicatedStorage.VEG1:GetChildren()
		local mesh = meshDirectory[math.random(1, #meshDirectory)]:Clone()
		mesh.Parent = workspace
		mesh.CFrame = CFrame.new(x,y*2 + 28,z)
	end
end

function mountLayer(x,heightY,z,material)
	local begY = -baseHeight
	local endY = heightY
	workspace.Terrain:FillBlock(CFrame.new(x*4+2, (begY+endY)*4/2, z*4+2), Vector3.new(4, (endY-begY)*4, 4), material)	

	
	if material == Enum.Material.Grass then
		addVegetation(x*4+2, (begY+endY)*4/2, z*4+2)
	end

end
 
function makeChunk(chunkX,chunkZ)
	local rootPos = Vector3.new(chunkX*chunkScale,0,chunkZ*chunkScale)
	chunks[chunkX][chunkZ] = true -- Acknowledge the chunk's existance.
	for x = 0,chunkScale-1 do
		for z = 0,chunkScale-1 do
			local cx = (chunkX*chunkScale) + x
			local cz = (chunkZ*chunkScale) + z
			local noise = math.noise(generationSeed,cx/xScale,cz/zScale)
			local cy = noise*baseHeight
			local materialChoosing = math.random(1,4)
									
			if cy <= 1 then
				if materialChoosing == 2 then
					mountLayer(cx,cy,cz,Enum.Material.Rock)
				else
					mountLayer(cx,cy,cz,Enum.Material.Grass)
				end
			else
				mountLayer(cx,cy,cz,Enum.Material.Rock)
			end
		end
	end
end
 
function checkSurroundings(location)
	local chunkX,chunkZ = math.floor(location.X/4/chunkScale),math.floor(location.Z/4/chunkScale)
	local range = math.max(1,renderDist/chunkScale)
	for x = -range,range do
		for z = -range,range do
			local cx,cz = chunkX + x,chunkZ + z
			if not chunkExists(cx,cz) then
				makeChunk(cx,cz)
			end
		end
	end
end
 
while true do
	for _,player in pairs(game.Players:GetPlayers()) do
		if player.Character then
			local torso = player.Character:FindFirstChild("Torso")
			if torso then
				checkSurroundings(torso.Position)
			end
		end
	end
	wait(1)
end

Thanks for your time

2 Likes