I tried to load animation but said attempt to index nil with humanoid

local tool = script.Parent

local player = game:GetService(“Players”).LocalPlayer

local character = player.Character

local humanoid = character.Humanoid

local animation = script.Animation

local animationtack = humanoid:LoadAnimation(animation)

tool.Activated:Connect(function()

animationtack:Play()

end)

:says attempt to index nil with humanoid

2 Likes

That error is happening because the game is not finding the humanoid when you try to assign the variable.

To fix this, you can try

local humanoid = character:WaitForChild("Humanoid")

If this still does not work, let me know.

The issue is on line 3:

local character = player.Character

The issue is that the player’s character hasn’t been created. The most common workaround for this is:

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

The or statement will do the second condition if the first condition (player.Character) is false, or in this case, nil. player.CharacterAdded:Wait() mentions an event, .CharacterAdded and uses :Wait() so the code will yield until the character is added and return the character once it is added. I see @BitMaster32 beat me to responding but this is a more common approach I see so I will still post this.

1 Like

tried it now it says attempt index nil with waitforchild

Hmmm. Yeah it can be kind of annoying sometimes.

Refer to @RoloTheDevBunny’s post. I think his is a better way of doing it.

after i did this now says humanoid is not part of model

Replace both your character and humanoid lines with our updated versions. Tell us the results.

theres no errors but the animation doesnt load

Try putting a print statement in the tool.Activated function to verify it is running.

1 Like

I’ve had this same issue before, don’t remember exactly how I solved it, but it was really irritating.

yeah i baso rewrite the whole script had to change it

Yeah that’s kinda lame. I’m glad it works now.

You need to use local script for player and also try this:

local tool = script.Parent

local player = game:GetService(“Players”).LocalPlayer

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

local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")

local animation = script.Animation

local animationtack = Animator:LoadAnimation(animation)

tool.Activated:Connect(function()

animationtack:Play()

end

Is the script a local script?

If it is:

local Tool = script.Parent
local Anim = script.Animation -- Your animation here

local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")

local Animation = Animator:LoadAnimation(Anim)

Tool.Activated:Connect(function()
	Animation:Play()
end)

Else, if your script is not a local script do this:

local Tool = script.Parent
local Anim = script.Animation -- Your animation here

Tool.Activated:Connect(function()
	local Character = Tool.Parent -- The parent of the tool. Activated only fires upon a player clicking with a tool, so the tool needs to be equipped. Which means the parent of the tool is your character
	local Humanoid = Character:WaitForChild("Humanoid")
	local Animator = Humanoid:WaitForChild("Animator")

	local Animation = Animator:LoadAnimation(Anim)
	
	Animation:Play()
end)

Hope this helps you