Optimization help

Hey! I’m fairly new to optimization and I would like to optimize my mining code as it’s relatively slow.

https://streamable.com/l1mvbg

The problem is the lag and how slow it is but I got no clue on how to speed it up.

-- Reveal adjacent
function Module:RevealAdjacent(physicalBlock: Model | BasePart)
    local pos = physicalBlock:GetPivot().Position
    
    local strength = 5
    
    local massMoveParts, massMoveCFrames = {}, {}
    local newParts = {}
 
    for x = -strength, strength do
        for y = -strength, strength do
            for z = -strength, strength do
                    --[[
                    -- if x == 0 and y == 0 and z == 0 then continue end
                    if 
                        (math.abs(x) == math.abs(z) and (math.abs(x) < strength or math.abs(z) < strength)) or 
                        (math.abs(x) == math.abs(y) and (math.abs(x) < strength or math.abs(y) < strength)) or 
                        (math.abs(z) == math.abs(y) and (math.abs(z) < strength or math.abs(y) < strength))
                    then
                        continue 
                    end]]
 
                local toBlockSpace = pos + Vector3.new(x, y, z) * 6
 
                Module.Blocks[pos.X][pos.Y][pos.Z] = 'Existed before'
 
                if (toBlockSpace - pos).Magnitude / 6 > strength then continue end -- Outside shaping
                if (toBlockSpace - pos).Magnitude / 6 < strength - strength / 2 then 
                    if 
                        Module.Blocks[toBlockSpace.X] and 
                        Module.Blocks[toBlockSpace.X][toBlockSpace.Y] and 
                        Module.Blocks[toBlockSpace.X][toBlockSpace.Y][toBlockSpace.Z]
                    then
                        local block = Module.Blocks[toBlockSpace.X][toBlockSpace.Y][toBlockSpace.Z]
                        if block.Block then
                            block.Block:Destroy()
                        end
                        
                        Module.Blocks[toBlockSpace.X][toBlockSpace.Y][toBlockSpace.Z] = 'Existed before'
                    else
                        local _, _ = Module.createXandYModels(toBlockSpace.X, toBlockSpace.Y, false)
                        Module.Blocks[toBlockSpace.X][toBlockSpace.Y][toBlockSpace.Z] = 'Existed before'
                    end
                    
                    continue 
                end -- Inside shaping
 
                local block =
                    if 
                    Module.Blocks[toBlockSpace.X] and
                    Module.Blocks[toBlockSpace.X][toBlockSpace.Y] and
                    Module.Blocks[toBlockSpace.X][toBlockSpace.Y][toBlockSpace.Z]
                    then
                    Module.Blocks[toBlockSpace.X][toBlockSpace.Y][toBlockSpace.Z]
                    else 
                    false
 
                -- if not block then continue end
            
                if block == 'Existed before' then continue end
    
                if (block and block.Block == nil) or (not block) then
                    if block and block.Ignore == true then continue end
 
                    local xModel, yModel = Module.createXandYModels(toBlockSpace.X, toBlockSpace.Y, true)
 
                    if not block then
                        if toBlockSpace.Y >= 0 then continue end
 
                        Module.Blocks[toBlockSpace.X][toBlockSpace.Y][toBlockSpace.Z] = {
                            Block = nil,
                            Type = Module.assignBlock(toBlockSpace)
                        }
                    end
 
                    -- print(Module.Blocks[toBlockSpace.X][toBlockSpace.Y][toBlockSpace.Z].Type)
                    local p = game.ReplicatedStorage.Blocks:FindFirstChild(
                        Module.Blocks[toBlockSpace.X][toBlockSpace.Y][toBlockSpace.Z].Type
                    ):Clone()
 
                    if not p then
                        p = Instance.new('Part')
                        p.Size = Vector3.one * 6
                        p.Position = toBlockSpace
                        p.Anchored = true
                        p.BrickColor = BrickColor.random()
                    end
 
                    table.insert(massMoveParts, p)
                    table.insert(massMoveCFrames, CFrame.new(toBlockSpace))
                    
                    table.insert(newParts, {
                        Parent = yModel,
                        Part = p
                    })
 
                    Module.Blocks[toBlockSpace.X][toBlockSpace.Y][toBlockSpace.Z].Block = p
 
                    -- Module.destroyingEvent(p)
                else
                    -- print('Already visible')
                end
            end
        end
    end
    
    workspace:BulkMoveTo(massMoveParts, massMoveCFrames)
    
    for _, v in newParts do
        v.Part.Parent = v.Parent
    end
end
2 Likes