I am working on a chunk system for my mining game but it causes lag. I only have certain events for adding blocks like when mining a block or the mine is made. It creates the first 50 layers of blocks above you and first 50 under you. It works fine but like I said, it causes lag. I have a script that’s deleting any blocks that are not in that range but its not helping.
Script:
--Load Chunk
local PlayerDepth = math.floor(Humanoid.Torso.CFrame.Y)
for _,BlockData in ipairs(MineData) do
--Check Position
if PlayerDepth - (ChunkSize*BlockSize)/2 > BlockData.BlockPosition.Y then
break
end
--Top
if BlockData.BlockPosition.Y - PlayerDepth <= (ChunkSize*BlockSize)/2 and BlockData.BlockPosition.Y - PlayerDepth > -1 then
--Check if block exists
local BlockExists = false
for _,CurrentBlock in pairs(Mine:GetChildren()) do
if CurrentBlock.Position == BlockData.BlockPosition then
BlockExists = true
break
end
end
if not BlockExists then
local NewBlock = BlockData.BlockType:Clone()
NewBlock.Position = BlockData.BlockPosition
NewBlock.Parent = Mine
wait()
end
--Bottom
elseif PlayerDepth - BlockData.BlockPosition.Y <= (ChunkSize*BlockSize)/2 and PlayerDepth - BlockData.BlockPosition.Y > -1 then
--Check if block exists
local BlockExists = false
for _,CurrentBlock in pairs(Mine:GetChildren()) do
if CurrentBlock.Position == BlockData.BlockPosition then
BlockExists = true
break
end
end
if not BlockExists then
local NewBlock = BlockData.BlockType:Clone()
NewBlock.Position = BlockData.BlockPosition
NewBlock.Parent = Mine
end
end
end