Attempt to index nil with 'FindFirstChild' error but the script works?

So I made a sprinting local script. The script itself works as it should but I keep getting this error:

Players.Zombie2358.PlayerGui.Sprint:23: attempt to index nil with 'FindFirstChild'  -  Client - Sprint:23

But the script works.
Here is the part which shows the error:

22  local charhum = char:FindFirstChild("Humanoid")
23  local animator = charhum:FindFirstChild("Animator")
24  local loadedAnim = animator:LoadAnimation(animObj)

I don’t know if I did something wrong or if it is a Roblox issue because the script manages to find Animator and works as expected. Any feedback would be appreciated.

Try replacing :FindFirstChild(“Humanoid”) with :WaitForChild(“Humanoid”) there’s always the possibility the script is trying to reference the humanoid before it’s loaded causing it to return nil

now it says “attempt to index nil with ‘WaitForChild’”. But the script still works…

Nevermind, I have managed to fix it by locating the Humanoid at the beginning of the script.