Attempt to index nil with 'LoadAnimation'

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    When the user clicks “C” on their keyboard, the player’s character plays the animation

  2. What is the issue? Include screenshots / videos if possible!
    I receive the error Players.EncryptedBloxy.PlayerScripts.LocalScript:12: attempt to index nil with 'LoadAnimation'

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    nothing so far

local userInputService = game:GetService("UserInputService")
local crawlAnimation = game.Workspace.Animations.crawl
local char = game.Players.LocalPlayer.Character

if char then
	humanoid = char:WaitForChild("Humanoid")
end


userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.C then
        humanoid:LoadAnimation(crawlAnimation):Play()
    end
end)

This is a LocalScript inside StarterPlayerScripts.

2 Likes

Try to write :

humanoid:WaitForChild("Animator"):LoadAnimation(crawlAnimation):Play()
2 Likes

I recieved the error attempt to index nil with 'WaitForChild' with both WaitForChild and FindFirstChild

1 Like

Ok so maybe try this I know it’s will change nothing but anyway :

local userInputService = game:GetService("UserInputService")
local crawlAnimation = game.Workspace.Animations.crawl
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.C then
		humanoid:WaitForChild("Animator"):LoadAnimation(crawlAnimation):Play()
	end
end)
1 Like

I get an Infinite yield possible on 'Players.EncryptedBloxy.PlayerScripts:WaitForChild("Humanoid") but replacing the WaitForChild for a FindFirstChild makes the original error appear.

1 Like

Put the local script on the StarterCharacterScripts

That was the fix, I also realised the animation was done in R6 and my avatar was in r15 so that was probably the 2nd problem. Thank you none the less :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.