How to make a basic mining system

Hello Developers,

You have often seen mining, where you dig blocks, and they have seperate health for each block too
Something lke this:


Now, we will learn how to do it

Required knowledge:

:blue_square: This is a very easy tutorial, but you need to know the basic mentioned above

Step 1: Making block

You need to keep in mind, all blocks should be same size, or it will be hard to make a basic system, it will be more like irl where random blocks are there
image

Now after making your block, add two values in the block

  • Int Value
  • Int Value

You need to add 2 values, 1 for current health of block, 2 is maximum health of block
image

Re-name your block with a name, all your blocks which can be mined should have the same name
And make sure your HP and max hp of your block have same health, later the HP will changed
but the max hp will remain unchanged

Step 2: Making Health of block gui

You can make it however you like to, but I am making the UI like this:
image

Note that your block should have something like this:
image

Now we don’t want to show the health of block, place the billboard gui in ReplicatedStorage, do this for block too
image

Now we are 50% done, we need to make a shovel, and a script which spawns these mining blocks

If you don’t want to spawn blocks, and you want to keep them yourself at random positions, don’t do the script part

Step 3: Spawning blocks

Here is how it works

Now to make that, you need to know Position and Loops
Script:

local sand = game.ReplicatedStorage.MineBlock -- your block

for x = 1,100,5 do -- first value is starting position of x, second is last position of x
    for z = 1,100,5 do -- same thing but position of z instead of x
        task.wait(0.02)
        local clone = sand:Clone() -- clone brick and keep it in workspace
        clone.Parent = workspace
        clone.Position = Vector3.new(x,2.5,z)
    end
end

This is just incase if you don’t know starting pos of x


Read this image carefully, if you need to generate blocks from a particular block/position to another position/block, every position have x,y,z

image
Starting block
image
Ending block

You have 2 positions 10,22, now the script generates blocks between 10 and 22
For that you need to do this in the script I mentioned:

for x = 10,22,2

you need to however take the L.C.M as third number

If you keep 5, then you get error, as if you add 5 to 10, then you get 15,then you get 20,then you get 25
But maximum is 22, so you should choose L.C.M

Step 4: Making shovel

As mentioned, this tutorial is not for telling how to make a tool, its about how to make a tool which can dig the blocks

So make your custom tool, like a shovel

image
Add a local script in your tool

Script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
    if script.Parent.Parent == player.Character then
        if mouse.Target.Name == "MineBlock" then
            local target = mouse.Target
            if target.HP.Value == 1 then -- if the block have 1 hp, then it gets deleted
                target:Destroy()
            else -- if block have more than 1 hp, then its hp goes down by 1
                target.HP.Value -= 1
                local guiclone = game.ReplicatedStorage.BillboardGui:Clone()
                guiclone.Parent = target -- the health gui
                guiclone.Frame.StatusFrame.Size = UDim2.new(target.HP.Value/target.MaxHP.Value,0,1,0) -- this line makes the health bar of the block the player is mining
            end
        end
    end
end)

However, you cannot understand any of the above scripts, if you don’t know basics, and you also need to understand how this script works

Tutorial is Done
Enjoy!

Note: This is client-based system, if you mine a block, it only dissappears for you, not for others
Check this DevHub article on Remote Events to make it server-based

12 Likes

Why not use humanoid for the maxhealth and health? You wouldn’t need to really script it if you did that.

Or atleast attributes

2 Likes

You need to create an attribute for each block, and thats harder than this so I did not mention it

So wa humanoid?
They’re the same but you don’t need to script the health system.

Why are you cloning this? Wouldn’t this mean that there are 100 useless GUI elements when you actually break the block? This seems pretty inefficient, and I think it might cause some lag for some players.

Nope, when you break the part, the gui, and the part goes off

Humanoid is not used much for this kind of system

1 Like

Humanoids are very expensive and having hundreds of blocks each with their own humanoid makes it worse, humanoids should only be used when making NPCs that will move and do animations, but for blocks like this, all you need is to hold a reference to the block with its health or have an attribute or value within the block.

I also agree with you stating that there is no reason for cloning them every time that they hit the block. Instead, the poster should reuse the same GUI that’s within the block

4 Likes

@regexman, humanoids are… the worst option you could use for a case like this. They’re insanely resource intensive and should be used only if you need them.


As for the tutorial, attributes would be more efficient than value objects, as they don’t create an entire separate instance.

You don’t. Once an attribute is created for an instance, cloning the part will have existing attributes cloned as well.

Cloning a gui for each part is really inefficient as @Judgy_Oreo stated. Instead, you should cache one element and parent it to the part you’re mining.

2 Likes