How to make a breakable part that breaks with tool

Hello. I know there have been topics about this before but none helped me. So I decided to make a new topic.
So what I’m trying to do is make a part that’s able to be broken by a “Pickaxe” tool.
Like the player finds this part in their way so what they can do is take their pickaxe and break the rock that’s in their way.

Here is my current code:

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

mouse.Button1Down:Connect(function()
    if mouse.Target ~= nil and mouse.Target:FindFirstChild("Breakable") then 
	    mouse.Target:Destroy()
    end
end)

By the way I have a value instance inside of the breakable part that’s called “Breakable” This is what the script is referring to.

Thanks!

4 Likes

Tool.Activated is a good way to do this.

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

Tool.Activated:Connect(function()
       if mouse.Target ~= nil and mouse.Target:FindFirstChild("Breakable") then 
	    mouse.Target:Destroy()
    end
end)

If you have any questions regarding Tool.Activated here is an article

I would also recommend using remote events, how you have it set up now is the part only breaks for you and not anyone else.

2 Likes

I’m pretty sure mouse.target does not return “nil”, if I remember correctly it returns “air” it is is not on a part.

1 Like

It works ok, but it doesnt support for mobile players

1 Like

I sure that you use this:

mouse.Button1Down:Connect(function()
    if script.Parent.Parent:IsA("Model") then
    if mouse.Target ~= nil and mouse.Target:FindFirstChild("Breakable") then 
	    mouse.Target:Destroy()
    end
   end
end)
1 Like

OnTouched inside of the part you want to break should be good.

1 Like

Maybe make the part have humanoid and when the tool is activated just reduce the part health until it died and break the part

1 Like
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:Connect(function()
    if mouse.Target and mouse.Target:FindFirstChild("Breakable") then 
	    mouse.Target:Destroy()
    end
end)

By the way doing that from a Local Script won’t change anything on the Server, which means if you break a block on your screen it will only show on your screen and not on the other players screens. What I would recommend for that is using Remote Events to fire and event to the server and that server will make it break.

1 Like

Try this code:

local UIS = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()

if (UIS.KeyboardEnabled) then--checks if player has a keyboard
    print("Player has keyboard")
    tool.Activated:Connect(function()
	    if mouse.Target ~= nil and mouse.Target:FindFirstChild("Breakable") then 
		    mouse.Target:Destroy()
	     end
    end)
else-- runs if player does not have a keyboard
    print("No keyboard found")
    tool.Equipped:Connect(function()
	    mouse.Button1Down:Connect(function()
		    if mouse.Target ~= nil and mouse.Target:FindFirstChild("Breakable") then 
			    mouse.Target:Destroy()
		     end
	    end)
    end)
end

If there are any errors tell me. Note: Like people have been saying, you should make it destroy on a server script by using a remote event so it breaks for everyone. I only recommend this if you want the part to break for everyone though.

1 Like

Thats too much code for nothing and will eventually add delays and connection losses for nothing my code does the exact same thing, to check if a Target is here you could simply do if mouse.Target then.

1 Like

Okay thanks but I think your code runs all the time even without the tool equipped.

1 Like

Thanks everyone for the help! And I’m not worrying about server versus client because the game I’m gonna use this for is single player. I will try a few of these ideas ans get back to you all. Thanks!

1 Like
local UIS = game:GetService("UserInputService")
local Tool = script.Parent
local Mouse = game.Players.LocalPlayer:GetMouse()

Tool.Activated:Connect(function(Input, Chatting)
		Mouse.TargetFilter = game.Players.LocalPlayer.Character
		local Target = Mouse.Target
		if Target ~= nil then --If is pointing to something
			local PointingPart = Mouse.Target
			if PointingPart:FindFirstChild("Breakable") then
				game.Debris:AddItem(PointingPart,0)
			end
		end
end)

I have tried it and this works in a LocalScript, child of tool.

3 Likes

Thank you so much for this! It worked perfectly! Thanks!

1 Like

It does support with mobile players