How to make a terrain digging tool?

I am making a survival style game, at the moment i need to know how to make a digging tool to dig terrain.

Does anyone know how i can do that?

PS: I am NOT asking for full scripts.

5 Likes

To dig actual terrain or to dig blocks like in Minecraft?

To dig actual terrain.

Edit: Specifically i am looking for a tool, that when you swing, breaks terrain.

You can use Terrain:FillRegion() with the Air material to remove a part of the terrain.

3 Likes

The page doesn’t show any examples or usage of it.

How would i use it in the script?

It requires three parameters: a Region3, the resolution of the new topology, and the material. To use it in a script, you’d have to create a Region3 and insert it into the function. If you want to make a digging tool then you can cast a ray from the player, create a Region3 with your preferred size on the final position of the raycast, then use that Region3 in the FillRegion() function.

1 Like

Could you make an example on a script?

Again, i’m not asking for full scripts but understanding it just by text is really hard.

If you wanted to clear out more of a circular shape you can also use the following. I can test this when I get out of school in an hour.

workspace.Terrain:FillBall(--Position, Radius of sphere, Material)

Local script

local tool = script.Parent

local event = --define here

local equipped = false

tool.Equipped:Connect(function()
      equipped = true
end)

tool.Unequipped:Connect(function()
       equipped = false
end)

player:GetMouse().MouseButton1Up:Connect(function()
         if equipped == true then
             local mouseRay = player:GetMouse().UnitRay
             local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
             local ignoreList = {player.Character}
             local hit, hitposition = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
             if hit and hit:IsA("Terrain") then
                  event:FireServer(hitposition)
             end
        end
end)

Server Script

local size = 7

local event = --define here

event.OnServerEvent:Connect(function(player, position)
       game.Workspace.Terrain:FillBall(position, 5, Enum.Material.Air)
end)
9 Likes

I just edited it, I had a few errors before. I just tested it and it works! This would also work for the server script, you can play around with both and see which you prefer:


local size = 7 --change to how big you want it to be
game.ReplicatedStorage:WaitForChild("EVENT").OnServerEvent:Connect(function(player, position)
	workspace.Terrain:FillRegion(Region3.new(Vector3.new(position.X - size/2, position.Y - size/2, position.Z - size/2), Vector3.new(position.X + size/2, position.Y + size/2, position.Z + size/2)), 4, Enum.Material.Air)	
	
end)
2 Likes