How would I destroy a single part out of several on click through a single script?

Hello! After looking around for clues for a while I’m still failing at making this happen. What I’m trying to achieve is parts that upon being clicked will destroy itself. There are 100 of these parts and so I’d prefer not having 1 script for each individual part. I have a single script in the folder as you see pictured and I want that to be the script that destroys the part that has been touched. My early attempts just destroyed every single part in the folder on click which is the opposite of what I want. I also tried to have a single ClickDetector under the folder to minimize it even further but I couldn’t figure out how to make it delete the specific object being touched instead of all of them. Is doing this possible?

1 Like

This should work!

Just change the first line to where your RedMounds folder is! (I assumed it is in Workspace)

local RedMoundsFolder = workspace.RedMounds

for _,Mound in pairs (RedMoundsFolder:GetChildren()) do
	if Mound:FindFirstChild("ClickDetector") then
		Mound.ClickDetector.MouseClick:Connect(function()
			Mound:Destroy()
		end)
	else
		print "A mound does not have a ClickDetector!"
	end
end
4 Likes

Worked like a charm! Thank you :smile:

2 Likes