How can I modify my script so it can only a Humanoid can touch a part?

As seen in the video above, anything can make the Box teleport to a position.
I want to modify my code so only Humanoid can make the box teleport to a position.

local Box = game.Workspace.box
local Door = script.Parent

Door.Touched:Connect(function()
Box.Position = Vector3.new(-127, 20, -145)
end)
1 Like

You’ll need to find what specifically touched your part. You’ll need to search the part’s parent for a Humanoid, like so:

Part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
--Do stuff
end
end)

See the BasePart.Touched page for more info.

3 Likes

Correct me if i’m wrong but if someone (say, an exploiter) changed the name of the Humanoid instance to something else wouldn’t the script not work?

It is possible if it’s a local script. :FindFirstChild only searches for the .Name for each object under the Instance you index to.
Alternatively, you can loop inside of the character and use :IsA to see if the Class is a Humanoid.

1 Like

Possibly. Instead of using :FindFirstChild() you can use :FindFirstChildWhichIsA() or :FindFirstChildOfClass() to find a Humanoid object (by ClassName) inside of the character, regardless of the Humanoid’s actual Name.

1 Like