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

How to make x.Touched limited to humanoid?

Asked by 10 years ago
script.Parent.Touched:connect(doorOpening)

That is how the line works, with doorOpening being my function. But I want to make sure that only parts in a group with a humanoid will make that function work, rather than any part.

3 answers

Log in to vote
3
Answered by
TheMyrco 375 Moderation Voter
10 years ago
script.Parent.Touched:connect(function(p)
    if p.Parent:FindFirstChild("Humanoid") then
        --open door here
    end
end)

This only works if you don't rename the Humanoids.

Ad
Log in to vote
2
Answered by
Andalf 100
10 years ago
script.Parent.Touched:connect(function(hit)

end)

the Touched event provides an argument which we send to the function and give it the name hit, the argument is the object that triggered the event.

So in the case of limiting it to the humanoid we can do this.

if hit.Parent.FindFirstChild("Humanoid") then
    doorOpening()
end

That tells the game to check the parent of the object that hit me to find a humanoid, if the humanoid is found it is returned as an object, otherwise it returns false.

When put together

script.Parent.Touched:connect(function(hit)
    if hit.Parent.FindFirstChild("Humanoid") then
        doorOpening()
    end
end)
Log in to vote
1
Answered by 10 years ago

The best way and simplest, wich will make you learn & it works, is this one:

function onTouched(part) -- when the part is touched
local hum = part.Parent:findFirstChild("Humanoid") -- if the thing wich hits humanoid (can be model, and in this case it is, if has an member called Humanoid, wich declares Player)
if hum ~= nil then -- If HUMANOID exists
  -- Code here! (in this case doorOpening() )
  doorOpening() -- run the function of open door!
end
end

part.Parent.onTouched:connect(onTouch)

Answer this question