Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a part disappear only after being touched by a certain object?

The script below makes the part disappear after being touched by anything, which is a problem. But I don't know how I will make it so it will only disappear after being touched by a certain object.

 health = script.Parent.Parent.Parent.Health
local debounce = false

function onHit()
if debounce == false then
	debounce = true
	health.Value = health.Value - 10
	print (health.Value)
	wait(3)
	debounce = false
	script.Parent:Destroy()
	end
end

script.Parent.Touched:connect(onHit)

1 answer

✓ Accepted answer

Touched has an optional argument to see what touched it, for example if you wanted to see if the toucher had a Humanoid inside

function onHit(toucher)
	if toucher.Parent:FindFirstChild("Humanoid") then
		print("This part was touched by something with a humanoid inside")
	end
	
	-- or you could filter by name, etc
	if toucher.Parent.Name == "someSpecialPart" or toucher.Name == "someSpecialPart" then
		print("This part was touched by someSpecialPart")
	end
end

script.Parent.Touched:Connect(onHit)

This discussion is preserved as it was. New answers, comments, and votes are closed.