--local chunkHandler = require(game.ReplicatedStorage.Modules.Chunk_Handler)

local grid = chunkHandler:GetGrid()

local oldChunks = {}

repeat wait() until game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character.Parent == workspace


while wait(4) do
	local chunk = chunkHandler:GetChunkAtVector3(grid, game.Players.LocalPlayer.Character.HumanoidRootPart.Position)
	if chunk then
		local neighbors = chunk:GetNeighborChunks(grid, 3, true)
		
		
		local newChunks = {}
		
		for i,v in pairs (neighbors) do
			--[[local p = Instance.new("Part")
			p.Anchored = true
			p.CFrame = v.CFrame
			p.Size = Vector3.new(4, 4, 4)
			p.Parent = workspace
			game.Debris:AddItem(p, 2)]]
			
			newChunks[v] = v
			if oldChunks[v] == nil  then
				oldChunks[v] = v
			end
			v:ShowResources()
		end
		
		newChunks[chunk] = chunk
		chunk:ShowResources()
		
		
		
		for _, oldChunk in pairs (oldChunks) do
			if newChunks[oldChunk] == nil then
				--This chunk is no longer in range
				print("HIDING CHUNKS")
				oldChunk:HideResources()
				oldChunks[oldChunk] = nil
			end
		end
	end
end
