Attempt to index nil with Humanoid (Animation)

  • I’m in a LocalScript

So I’m trying to animate a “weapon” but it tells me an error saying that It can’t index with the Humanoid.

Here’s the error:

Here’s the script:

And I also tried this:

But I got a different bug. I don’t know what to do, and as ever I come here because I know there is a lot of good people in the dev forum that can help me :wink: , I’ll be very very VERY grateful if you can give me a hand with this (even if it is a little bit) :smile:

1 Like

Humanoid usually takes longer to load than the local script so it would usually return nil without WaitForChild() function (sometimes WaitForChild() returns “Infinite yield possible”, too). Therefore, if you still want to call it at the beginning of the script, I advise doing this:

local LoadedAnimation = player.CharacterAdded:Wait():WaitForChild("Humanoid"):LoadAnimation(Animation)

maybe :slight_smile:

The simplest fix for this is just to store the character in a variable and wait for it to load if it’s nil (It’s normaly nil when you first join the game)

local Character = player.Character or player.CharacterAdded:Wait()

and after that you get the Humanoid and the animator (all newer work should use the animator inside of the humanoid instead of the humanoid itself)

local Animator = Character:WaitForChild("Humanoid").Animator

and after that you can load all your animations

local AnimationTrack = Animator:LoadAnimation(AnimationInstance)

Documentation:

You need to load the Animaion on an AnimationTrack.

local AnimTrack
local AnimId = "whatverAnimId

script.Parent.Equipped:Connect(function()
     local Animator= player.Character.Humanoid.:FindFirstChildOfClass("Animator")
    if Animator then
           AnimTrack = Animator:LoadAnimation(AnimID)
           AnimTrack:Play()
      end
end)

Also, check if the Animation is made for r6 or r15. if its made for r6, change the defaultt CharacterType to r6, in the gameSettings.

if R15, then set the default char to R15

Correction, the Animator exists on the Humanoid, so the line would be: local Animator= player.Character.Humanoid.:FindFirstChildOfClass(“Animator”)

You set up the parameters wrong, try using this and maybe this will work.

local player = game.Players.LocalPlayer
local character = player.CharacterAdded or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:FindFirstChild("Animator")
local tool = script.Parent
local animation = tool.Animation
local animationTrack = animator:LoadAnimation(animation)

tool.Equipped:Connect(function()
    if animator then
        animationTrack:Play()
    end
end)