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.
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.
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)
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.
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.
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.
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!
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.