how do i get the humanoid that the player touched from a touched event
3 Likes
local Part = --Path to part
Part.Touched:Connect(function(Part)
if Part.Parent and Part.Parent:FindFirstChild("Humanoid") then
local Humanoid = Part.Parent:FindFirstChild("Humanoid")
end
end)
2 Likes
if i make the part the humanoid of the local player, will it still work?
1 Like
Why do you need to do such a thing? Also, no.
1 Like
You can use FindFirstAncestorWhichIsA()
to get the model (regardless of whether the touched part is a body part or accessory parts). Then from that, you can just get the humanoid from the model.
And while depending on the part’s parent being a model works, there is a possibility that the ‘Touched’ event detects an accessory’s part (which does not directly have the character’s model as its parent).
local part: BasePart
part.Touched:Connect(function(touch: BasePart)
local model = touch:FindFirstAncestorWhichIsA("Model")
local hum = model and model:FindFirstChildWhichIsA("Humanoid")
if hum then
-- do code
end
end)
7 Likes