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.
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChildOfClass("Humanoid")
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()
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.