Error getting the character and humanoid

Every time I try to get the character it always gives me the same error anyone know a solution?
Error: https://gyazo.com/fead01746f59163bd6b3e3da7f1128be
script:

local tool = script.Parent

local animFolder = tool.Animations

local holdAnimation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(animFolder.HoldAnimation)

tool.Equipped:Connect(function()
	 holdAnimation:Play()
end)

That’s because the Character doesn’t exist yet.
You can simply do:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local holdAnimation = Character.Humanoid:LoadAnimation(animFolder.HoldAnimation)

to wait until the character has been created

1 Like

I forgot of the CharacterAdded event fires before the humanoid is added or after the humanoid is added.

If it’s before, then you should also create a seperate variable for humanoid where you use :WaitForChild() like this:

local Character = -- this is the same as the above reply
local Humanoid = Character:WaitForChild("Humanoid")
local holdAnimation = Humanoid:LoadAnimation(animFolder.HoldAnimation)

Thanks for the help, although I’m still confused this same script used to work perfectly and now it doesn’t?

It was probably just a race condition. If the character was loaded before the tools were cloned into your backpack it would have worked, if not it’d error.

1 Like