Cannot load the AnimationClipProvider Service happens after player dies and character loads back in

I have a local script placed in a folder in StarterPack, and it plays an idle animation when the character loads in. Everything works fine when testing it in roblox studio, but when I test my game out by joining it from the roblox website, sometimes it breaks after dying, and it gives the error
Cannot load the AnimationClipProvider Service.
I have no idea why any help would be appreciated
Code:

local Player = game.Players.LocalPlayer
local Character
repeat task.wait()
	Character = Player.Character
until Character ~= nil

repeat task.wait() until game:IsLoaded()


local Humanoid = Character:FindFirstChild("Humanoid")
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local AnimationsFolder = script.Parent:WaitForChild("Animations")

local EndCD = 2
local PerAttackCD = .3
local Active = false
local Values = script.Parent:WaitForChild("Values")
local AttackRound = Values.ComboRound
local AMOUNT_OF_ATTACKS = AttackRound.MaxComboRound.Value

local UserInputService = game:GetService("UserInputService")

local ServerHandler = script:WaitForChild("ServerHandler")

local Debris = game:GetService("Debris")

local HitboxModule = require(game.ReplicatedStorage.MainAssetBase.Modules.HitboxModule)

local function ToggleJumpAndSpeed(toggle: boolean, humanoid: Humanoid)
	if toggle == true then
		humanoid.WalkSpeed = 20
		humanoid.JumpPower = 50
	else
		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0
	end
end

if Humanoid and HumanoidRootPart then
	local Animator = Humanoid:WaitForChild("Animator")
	
	local IdleAnimation = Animator:LoadAnimation(AnimationsFolder:WaitForChild("IdleAnimation"))
	local AttackAnimation1 = Animator:LoadAnimation(AnimationsFolder:WaitForChild("AttackAnimation1"))
	local AttackAnimation2 = Animator:LoadAnimation(AnimationsFolder:WaitForChild("AttackAnimation2"))
	local AttackAnimation3 = Animator:LoadAnimation(AnimationsFolder:WaitForChild("AttackAnimation3"))
	local AttackAnimation4 = Animator:LoadAnimation(AnimationsFolder:WaitForChild("AttackAnimation4"))
	local AttackAnimation5 = Animator:LoadAnimation(AnimationsFolder:WaitForChild("AttackAnimation5"))
	
	IdleAnimation.Priority = Enum.AnimationPriority.Action
	IdleAnimation:Play()
end

Hey, I have fixed my own script throwing the same error just now. Try

repeat task.wait() until Player.Character
Character = Player.Character

So far for me it hasn’t errored. I hope it’s consistent and it works for everyone. :slight_smile:

Im pretty sure that error happens when the object you are trying to load does not exist in workspace yet. Waiting until the player exists in workspace should fix it.