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

How to make x.Touched limited to humanoid?

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

✓ Accepted answer
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.

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)

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)

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