Pickaxe not working

So me and my friend where making a pickaxe, for my game, it wont break a part when advised to. No errors are printing, and no part is breaking.

script.Parent.Touched.connect(function(destroy)
if destroy.Name == "Part" then
wait(.1)
destroy:Destroy()
end
end)

Me and @Pandoozle do not know the issue.

Instead of a colon, you are using a period to connect the listener to the touched event. The fixed code should work fine:

script.Parent.Touched:Connect(function(destroy)
if destroy.Name == "Part" then
wait(.1)
destroy:Destroy()
end
end)

image

2 Likes

Will test it!

Great explanation by Dandystan. Although one step further, I’d probably advise you to add a debounce to this code, just so you don’t have stuff running repeatedly every time the pickaxe collides with something.

Based on your code, this is what you would want:

local objects = {}

script.Parent.Touched:Connect(function(destroy)
if destroy.Name == "Part" and not objects[destroy] then
objects[destroy] = true
wait(.1)
destroy:Destroy()
end
end)

Although it probably won’t affect much, this is just so it doesn’t run repeatedly on the same object.

1 Like

This code will produce a memory leak, remember to remove the instance from the table:

local objects = {}

script.Parent.Touched:Connect(function(destroy)
if not objects[destroy] and destroy.Name == "Part" then
objects[destroy] = true
wait(.1)
objects[destroy] = nil -- clean up
destroy:Destroy()
end
end)

It’s also much faster to check if the part is in the table than check the part’s name, so I moved that. I would also recommend using a name more unique than “Part” unless you want everything to be mineable.

3 Likes