Attempt to Index nil with 'WaitForChild'

I’m trying to make a function that will play an animation when you press the F key. However, I keep getting an error that says "attempt to index nil with ‘WaitForChild’ " on line 5. This is all done in a LocalScript in StarterPlayerScripts. Any help is appreciated.

local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")
local anim = script:FindFirstChild("Animation")
local playanim = humanoid:LoadAnimation(anim)

	if input.KeyCode == Enum.KeyCode.F then

Since it’s in StarterPlayerScripts, it will run before the character initially loads. So char = player.Character will most definitely be nil at first. You’ll need to write code to handle the character respawning and loading it per humanoid.

The simplest solution to this is to just move this script into StarterCharacterScripts.

I tried moving it to StarterCharacterScripts, but it gives me an error saying “LoadAnimation requires an Animation object”.