I am having trouble with an animation

Hey there,

I am currently developing a game and are having trouble with playing an animation.

The error:

Attempt to index nil with ‘LoadAnimation’ - Server - Main:8

The Script:

local repStorage = game:GetService("ReplicatedStorage")
local animationID = 7103619501

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		repStorage.Events.KnifeSwingEvent.OnServerEvent:Connect(function()
			local humanoid = character:FindFirstChild("humanoid")
			local animation = humanoid:LoadAnimation(animationID)
			animation:Play()
		end)
	end)
end)

I’m not sure as to why the output is saying index nil with ‘LoadAnimation’.

Thanks for the assistance! :grin:

This should be “Humanoid”.

Also Humanoid:LoadAnimation is now deprecated. It is recommended to use the animator inside the humanoid instead.

1 Like

So if the Humanoid:LoadAnimation function is now deprecated, I assume :Play should work.

When this occurs however, the animation still doesn’t play.

Got ya. Insert an Animation Instance in the Script, and then change the Script to this code:

local repStorage = game:GetService("ReplicatedStorage")
local animation = script:WaitForChild("Animation") -- name of animation instance

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
		repStorage.Events.KnifeSwingEvent.OnServerEvent:Connect(function()
			local humanoid = character:FindFirstChild("Humanoid")
			humanoid.Animator:LoadAnimation(animation)
			animation:Play()
		end)
	end)
end)

Hope it helped!


Plus it’s not the simple ID, you sometimes have to make

"rbxassetid://"..animationID
1 Like

I have done what you’ve said, but there are still no results. The output won’t give any errors and the animation still doesn’t play.

Try putting a print after the calling event function. If it doesn’t prints, you might wanna check the FireServer() script.

1 Like

Alright, it does seem to be something with the :FireServer() script.

But I am unsure as to why this is as it was working before.

local UserInputService = game:GetService("UserInputService")
local repStorage = game:GetService("ReplicatedStorage")

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		repStorage.Events.KnifeSwingEvent:FireServer()
	end
end)
local repStorage = game:GetService("ReplicatedStorage")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7103619501"

repStorage.Events.KnifeSwingEvent.OnServerEvent:Connect(function(player)
    local humanoid = player.Character.Humanoid
	local track = humanoid.Animator:LoadAnimation(animation)
    track:Play()
end)

Is it a LocalScript? Is it inside one of the following folders: StarterGui, StarterPack, StarterPlayerScripts, possibly StarterCharacterScripts?

Edit: the function works just fine for me.