Localscript not recognizing a humanoid

Hello people of devforum, I have an issue where I would like to play an animation through a localscript, but to play that animation, I would need to define the humanoid. For some reason, it is not recognizing the humanoid (I grabbed it from the localplayer.Character). Reference pictures below.

https://gyazo.com/3e9ce128bc65266db3bed1b6d6c5a621
https://gyazo.com/5658dd106f6d565792a42bb79dda2b9b

try this instead

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChildOfClass("Humanoid")
1 Like

image
image

(For easier reference :wink:)

Try not to assume that the Character is already in motion by doing Player.Character.Humanoid, cause sometimes the Character doesn’t load fast in time properly before the LocalScript runs to detect it, which returns back as a nil value

What we use is the CharacterAdded:Wait() Event, which yields for a Character Model if our first check doesn’t properly find the Character in time

All you just need to do is add a Character variable like so:

local Char = player.Character or player.CharacterAdded:Wait() -- The "or" is VERY important in this

And replace your Hum line with this:

local humanoid = Char:WaitForChild("Humanoid")

It’s always better to yield for instances rather than indexing them

Also side note, but please consider using the Animator of the Humanoid instead, Humanoid:LoadAnimation() is deprecated and should be replaced with Animator:LoadAnimation()

1 Like

Thank you! this fixed the issue!

Try not to assume that the Character is already in motion by doing Player.Character.Humanoid, cause sometimes the Character doesn’t load fast in time properly before the LocalScript runs to detect it, which returns back as a nil value

If the character hasn’t loaded you’ll get an ‘attempt to index nil with Humanoid’ error and if the humanoid hasn’t loaded you’ll get a ‘no member named Humanoid’ error.