Making a Part find a child onTouch?

I’m trying to figure out when a part gets touched, it has to find a child of the part it got touched by.

  1. What do you want to achieve? I want a part to explode and disappear, but it needs to find a child in the part (humanoid) it has been touched by for it to run the code. If it doesn’t have the certain child, it won’t run.

  2. What is the issue? I am not sure how to make the script do that.

  3. What solutions have you tried so far? I have looked for solutions to find the child of the part it was touched by, but any I find doesn’t seem to work.

Here’s an example of what I am trying to achieve:

(mods, if my topic is wrong change it please)
Thank you!

1 Like

Sounds like a simple
FindFirstChildOfClass

1 Like
local part = script.Parent --Part which is being touched.

part.Touched:Connect(function(hit) --Part which caused the `Touched` event to fire.
	local humanoid = hit:FindFirstChildOfClass("Humanoid") --Search for a "Humanoid" instance inside the instance "hit" represents.
	if humanoid then --Check if a humanoid was located.
		--Do code.
	end
end)

This will work if the humanoid is inside the BasePart instance (as you described) which caused the Touched event of some other BasePart to fire. If the humanoid is in fact inside a model instance you’ll need to do the following.

local part = script.Parent

part.Touched:Connect(function(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitHuman = hitModel:FindFirstChildOfClass("Humanoid")
		if hitHuman then
			--Do code.
		end
	end
end)
2 Likes

It works! Thanks for the help!