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

How do I find out if whatever is touched is actually a body part?

Lets say that I have a touched event:

local part = script.Parent

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
	-- do whatever
	end
end)

This would work for things like body parts, but if I added an effect (lets say like a translucent part or sphere or something) and parented it to the character, then it would also think that the effect is also a body part. What is the best/most efficient way to stop this? My current solution is parenting the effect to the characters head or something, so it doesn't detect my effect as a body part.

2 comments

0

If hit:IsA("BasePart") then (checks whether its a part) replace BasePart with MeshPart if you use custom characters with meshes

0

what if my character is made of baseparts and so are my effects? and is my solution good enough?

2 answers

I'm not sure what you mean by effect, since particle emitters or lights cannot trigger the touched event. But if you don't want a part to be touched, you can turn off the CanTouch property for that part.

2 comments

0

Oh i am talking about if the effect is a part, like for an example a sphere, but if there was a touch detecter then the sphere would act as a body part which I DONT want

0

I believe I've already answered that then. Do accept my answer if it worked.

Maybe check the children inside of that instance. Like if the instance has children similar to a body part, you just claim it as a body part. Like this code example:

local Part = -- the part in the workspace which is going to be handling the event
Part.Touched:Connect(function(hit)
if hit:FindFirstChild("Anything similar to a body part inside of it (Motor3D, etc)") then
print("Print whatever inside of here")
end
end)

Sorry if it's too messy. But I think that will work for you.

1 comment

0

Also this statement can be used with a and statement with the if statement to check if the particle emmiter is in there, or sparkles.

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