✓ Accepted answer
Check if hit is a member of a humanoid.
You can do this by checking if the part's parent has a humanoid.
"How?"
Use an if statement!
First, make sure that the part exists. This is not necessary but I like doing so.
function onTouched(hit)
if hit then
hit:BreakJoints()
end
end
script.Parent.Touched:connect(onTouched)
There.
Now we need to see if the "hit's" parent has a Humanoid. We can check both with one if statement by using the keyword and. Like so,
function onTouched(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") then
hit:BreakJoints()
end
end
script.Parent.Touched:connect(onTouched)
The above only breaks the joints of things that don't have a humanoid. This would still break the joints of an NPC and such, but that shouldn't be a problem for you. If it is however,
Check for the hit's parent's name in Players.
Just add this to the if statement.
function onTouched(hit)
if hit and hit.Parent:FindFirstChild("Humanoid") and game.Players:FindFirstChild(hit.Parent.Name) then
hit:BreakJoints()
end
end
script.Parent.Touched:connect(onTouched)
There's a slight chance that the NPC will have the same name as a player but we'll assume this problem doesn't exist as it's really rare.
That should work.
Hope that helps.
Good Luck!
2 comments
What should the script's parent be?
The part that's in workspace you want to monitor the touch event for.