Animations break on death

whenever the player dies, the animations break. ive been stuck on this issue for about a full day now and am not exactly sure what to do

code is below:

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local db = false
local block = false
local punch = 0

wait()
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local animation = Instance.new("Animation")

local function IsBlockDown()
	return UserInputService:IsKeyDown(Enum.KeyCode.F)
end

local function onInputBegan(input, gameProcessed)
	
	if IsBlockDown() then
		if block == false then
			block = true
			db = true
			animation.AnimationId = "http://www.roblox.com/asset/?id=7541918527" -- block
			local animationTrack = humanoid:LoadAnimation(animation)
			animationTrack:Play()
			wait(0.5)
			animationTrack:Stop()
			db = false
			wait(1)
			block = false
		end
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		
		if db == false then
			punch += 1
			if punch == 1 then
				db = true
				animation.AnimationId = "http://www.roblox.com/asset/?id=7541761982" -- punch1

				local animationTrack = humanoid:LoadAnimation(animation)
				animationTrack:Play()
				wait(0.75)
				db = false
			end
			
			if punch == 2 then
				db = true
				animation.AnimationId = "http://www.roblox.com/asset/?id=7541823790" -- punch2

				local animationTrack = humanoid:LoadAnimation(animation)
				animationTrack:Play()
				wait(0.75)
				db = false
				punch = 0
			end
		end
	end
end

UserInputService.InputBegan:Connect(onInputBegan)

Where is this LocalScript?

If the script isnt in player char scripts then move it there. If it is try this for your char variable

local character = player.character or player.CharacterAdded:wait()