Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Does touch event only activate when touched by a humanoid? [RESOLVED]

Asked by 9 years ago

Title, and if it doesn't, what can I use that registers being w/ a normal part?

3 answers

Log in to vote
1
Answered by 9 years ago

Touched event does just what it says, it activates when touched. This means it will work if anything touches it. Although, people do add another line of code to check if what touched it has a humanoid, because this is one of the best ways to know a players character has touched that part. Sometimes activating a script for a non-character could break it due to your code not being made FOR that part!

An example of a script that would work if you wanted to activated code for anything that touches it, look bellow:

script.Parent.Touched:connect(function()
    print("I WAS TOUCHED BY SOMETHING!!!")
end)

Now an Example to make sure that what touched you is someone who has a humanoid inside of them, most likely a Players character, look bellow:

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        print("Something with a humanoid touched me!!!"
    end
end)

Now a lot more is going on in the script that involves a Humanoid. Like, in parenthesis I have hit but some reason not in the first script..? Why didn't I use hit in the script for anything? This is because in the script looking for a Humanoid, I need to know where I want to look into, so in the parenthesis I would make any "Variable" that represents the object that has touched the part to activate the event. Now I know how to reference the object that touched it, I use that reference and go to it's model and then I used some other event, FindFirstChild() which is a safe way to find some child of another object and see if it's there or not without breaking!! So I found the Humanoid then I do whatever else I want and done!

Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

.Touched is activated when any (unanchored) part comes into contact. It's not just for humanoids -- if you want to limit it to only pay attention to humanoids, that's extra work that you do.

From http://wiki.roblox.com/index.php/Touched

Description: Fired when another object comes in contact with this object.

Log in to vote
-1
Answered by 9 years ago

Thanks guys. Really helpful.

Answer this question