Hello. So if I have a simple touched script like so:
lua
local part = script.parent
part.Touched:Connect(function(hit)
local char = hit.Parent:FindFirstChild("Humanoid")
if char ~= nil then
--do stuff
end
end)
What I'm trying to do is find the character no matter what the part touches. The "character" I'm looking for is a mob so I can't just check if it's in the players list.
So my question is how can I change this to always find the character that hit the part?
check if the Model has Humanoid like :
1 | if Hit.Parent:FindFirstChildOfClass( "Humanoid" ) ~ = nil then ... end |
this will find the humanoid even if his name is changed, and doesn't will work with normal bricks, only Models with Humanoids like Mobs and Players
Here is another way you could do this:
1 | if game.Players:GetPlayerFromCharacter(hit.Parent) then |
GetPlayerFromCharacter()
returns the player associated with the given character (assuming that the player is associated with that character). If the player is not associated with the given character, GetPlayerFromCharacter()
will return nil
.
01 | local function onTouched(hit) |
02 | local char = hit.Parent |
03 | if char then |
04 | if char ~ = nil then |
05 | --do the stuff |
06 | end |
07 | end |
08 | end ) |
09 |
10 | script.Parent.Touched:Connect(onTouched) |