LoadAnimation Error!

--// Variables
local Damage = script.Parent.Damage.Value
local Animation = script.Animation.Swinging
local sound = script.Sound.Swinging
local tool = script.Parent
local cooldown = script.Parent.Cooldown
local humanoid = tool.Parent:FindFirstChildWhichIsA("Humanoid")

--// Tool Events
tool.Activated:Connect(function()

      local anim = humanoid:LoadAnimation(Animation) -- This doesn't work
      anim:Play()
      print("Swing has activated")
      sound:Play()
      wait(cooldown)
      anim:Stop()
      print("Swing has deactivated")

end)

I get an error saying attempt to index nil with LoadAnimation. Any help would be appreciated.

2 Likes

Try checking if there is an animation id for the animation, or if you used the correct path to refer to this animation.

I checked id which is there, and it is the correct path I made sure to test and see where the humanoid was.

I think this might be the cause. You are trying to get humanoid from player.Backpack.

You should do:

local humanoid = game.Workspace:FindFirstChild(tool.Parent.Parent.Name).Humanoid

Error, Index nil with Humanoid. Should I add a WaitForChild("Humanoid").

Try printing print(tool.Parent.Parent.Name) if it’s not referring correctly, you might need to add or remove a Parent. WaitForChild() shouldn’t be what you need.

It prints out my name, so the that should be correct.

Try removing Humanoid from my line of code and check if it’s still nil.
Or use FindFirstChildWhichIsA("Humanoid") if Humanoid might be a custom name.

Yeah it’s still nil.

30 chars

Try putting the humanoid declaration inside the Activated function. Or else, maybe try using CharacterAdded()

That’s because the tool is not in the character when they join the game. It gets parented to the backpack.

When I equip it, it goes to my character.

you might try using declaring the humanoid, like you did, inside the equipped function?

I still get the error nil.

30 chars

But the variable is defined before that. It’s defined right when the server starts up(maybe it waits till it’s in the backpack if it’s in startertools), but it’s never defined when it gets equipped, unless you moved the humanoid variable in the activated function

Yeah I just moved the humanoid variable and it still is nil.

Then it’s most likely not finding it. Try to use waitforchild or just plain findfirstchild

Index nil with both of them.

30 chars

I think you should go with WaitForChild. It’ll instantly return the object if it exists anyway.
You see, when the character’s model is created, CharacterAdded() fires but the model doesn’t have a humanoid yet. I’ve seen that happen already.

Just realized WaitForChild solves nothing.
Maybe try this?

--// Variables
local Damage = script.Parent.Damage.Value
local Animation = script.Animation.Swinging
local sound = script.Sound.Swinging
local tool = script.Parent
local cooldown = script.Parent.Cooldown
local humanoid

--// Tool Events
tool.Activated:Connect(function()

      humanoid = tool.Parent:WaitForChild("Humanoid")
      print(humanoid)
      --If it doesn't print at all, humanoid isn't there and never gets added. Custom character problems?
      local anim = humanoid:LoadAnimation(Animation) -- This doesn't work
      anim:Play()
      print("Swing has activated")
      sound:Play()
      wait(cooldown)
      anim:Stop()
      print("Swing has deactivated")

end)

If it’s a localscript then clearly :WaitForChild() is the way to go. Technically speaking script.Parent won’t need a WaitForChild but anything that is inside of the parent may very well need it. Even if you don’t need it know that it’s always a good practice to use it when trying to get something such as this.