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