Why can't I get this on part touched to work?

It is simple but I just don’t really get it anymore
I wanted this part when collided with a specified part name it activates something

script.Parent.Touched:Connect(function(Hit)
	if Hit and Hit.Parent.Name == "TestPart" then
		Hit.Parent:Destroy()
		script.Parent:Destroy()
	else
	end
end)

I’ve struggled with this for quite a while now so thanks for anything

You can try print out the value of Hit and detect if it is able to detect anything when other part hit it. Another thing you can try is putting this script inside the ServerScriptService instead of putting it directly below the object and see if it show any differences.

1 Like

Understanding the Touched Event

The Touched event is an event that fires when a BasePart (such as a Part , WedgePart , or MeshPart ) collides with another object. This event takes one argument: the object that collided with the part.

Here’s an example script that uses the Touched event to detect when a part collides with another part named “TestPart”:

-- This script will destroy the part and the script's parent when the part collides with another part named "TestPart"
script.Parent.Touched:Connect(function(Hit)
	if Hit and Hit.Parent.Name == "TestPart" then
		Hit.Parent:Destroy()
		script.Parent:Destroy()
	end
end)

This script uses the Touched event of the Part object to detect when it collides with another object. The function connected to this event takes one argument, Hit , which represents the object that collided with the part.

Inside this function, we check if Hit is not nil and if its parent’s name is equal to "TestPart" . If both conditions are true, we call the Destroy method on both Hit.Parent and script.Parent , which will remove them from the game.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.