Optimization help

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

Watch Absurdly Ludicrous Mining - Roblox Studio 2024-04-16 22-21-38 | Streamable

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

It’s generally good practice to store, functions or variables that are going to be reused outside of a loop in another variable. Because you are calling the function table.insert() repeatedly inside your for-loop, it’s going to store and create a that function every frame using the global memory (this isn’t good).

Instead what you could do is, outside of the loop create a variable called local tableInsert = table.insert
this is called function localization, by doing this you can store a function once inside your local memory, instead of continuously storing and creating a function using the global memory every frame.

1 Like

That doesn’t really make a huge difference sadly

1 Like

Have you only tried to localize the table.insert function, or other repeated functions and variables inside your script? Vector3.new(), BrickColor.random(), toBlockSpace etc…

1 Like

Yeah I have, and there’s not much difference, those are more surface-level things, the lag comes from when cloning or moving parts/models.

1 Like

How to use BulkMoveTo() - #3 by JayO_X Perhaps this post can help you.

1 Like

I’m using BulkMoveTo already and it made no difference :sob:

1 Like

That’s not what I’m referring to, sorry if I was unclear , in the post if you scroll down to the last reply, there was an alternative to the positioning inside the BulkMoveTo function.

1 Like

I found out that it has nothing to do with the movement of the blocks, so I’m not entirely sure what slows it down so much.

1 Like

Perhaps try optimizing the module scripts you are using?

1 Like

Could entirely be because the server handles all of it

1 Like