Chunk loading system assistance required

Hey!

I’m making a chunk loading system for my group due to one of our games having a lot of parts.

I got a chunk loading system working, but it causes a lot of lag when loading in another region. I used this system by TheNexusAvenger.

   ClassDContainment  = {"Sector2"},
   Sector2 = {"ClassDContainment","Sector22" },
   ClassDContainment2 = {"Sector22"},
   Sector22 = {"ClassDContainment2","Sector2"}
}

local ReverseChunks = {}
local ChunkBounds = {}
local ChunksVisible = {}
local ChunkModels = {}
local LastChunk = ""

local ChunksModel = game.Workspace:WaitForChild("SiteNavigation")
local Camera = game.Workspace.CurrentCamera
local HumanoidRootPart

for Name,ChunkTable in pairs(Chunks) do
   
   local m = Instance.new("Model")
   m.Parent = ChunksModel
   
   m.Name = "Temp" .. Name
   for i,v in pairs(ChunksModel:WaitForChild(Name):GetChildren()) do
   	v.Parent = m
   end
   ChunksModel:WaitForChild(Name):Destroy()
   m.Name = Name
   local Model = ChunksModel:WaitForChild(Name)
   ChunkModels[Name] = Model
   ChunksVisible[Name] = true
   ReverseChunks[Name] = {}
   for _,ChunkId in pairs(ChunkTable) do
   	ReverseChunks[Name][ChunkId] = true
   end
   
   local Position,Size = Model:GetModelCFrame().p,Model:GetExtentsSize()
   local PosX,PosZ = Position.X,Position.Z
   local SizeX,SizeZ = Size.X/2,Size.Z/2
   ChunkBounds[Name] = {PosX - SizeX,PosZ - SizeZ,PosX + SizeX,PosZ + SizeZ}
end

local function GetCameraInChunk()
   if not HumanoidRootPart then return end
   local Position = HumanoidRootPart.Position
   local PosX,PosZ = Position.X,Position.Z
   for ChunkName,Bounds in pairs(ChunkBounds) do
   	local X1,Z1,X2,Z2 = Bounds[1],Bounds[2],Bounds[3],Bounds[4]
   	if PosX > X1 and PosZ > Z1 and PosX < X2 and PosZ < Z2 then
   		print(ChunkName)
   		return ChunkName
   	end 
   end
end

local function SetChunkVisible(ChunkName,Visible)
   ChunksVisible[ChunkName] = (Visible == true and true or nil)
   ChunkModels[ChunkName].Parent = (Visible == true and ChunksModel or nil)
end

local function UpdateRenderedChunks(ChunkName)
   local ChunkData = ReverseChunks[ChunkName]
   for OtherChunkName,Visible in pairs(ChunksVisible) do
   	if Visible == true and ChunkData[OtherChunkName] ~= true then
   		SetChunkVisible(OtherChunkName,false)
   	end
   end
   
   for OtherChunkName,_ in pairs(ChunkData) do
   	if ChunksVisible[OtherChunkName] ~= true then
   		SetChunkVisible(OtherChunkName,true)
   	end
   end
   
   if ChunksVisible[ChunkName] ~= true then
   	SetChunkVisible(ChunkName,true)
   end
end

local Player = game.Players.LocalPlayer
local function CharacterAdded(Character)
   if Character then
   	HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
   end
end
CharacterAdded(Player.Character)
Player.CharacterAdded:connect(CharacterAdded)

while true do
   local ChunkName = GetCameraInChunk()
   if ChunkName and ChunkName ~= LastChunk then
   	LastChunk = ChunkName
   	UpdateRenderedChunks(ChunkName)
   end
   wait()
end```

Anyone knows a way to reduce this?
1 Like

Isn’t that just caused by the way your map is built? What does your map look like?

1 Like

We have the map divided in 4 parts, relatively small map at the moment, I just made one real quick for testing, all sorted in a folder.