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 :
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:
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
.
local function onTouched(hit) local char = hit.Parent if char then if char ~= nil then --do the stuff end end end) script.Parent.Touched:Connect(onTouched)