"LoadAnimation requires the Humanoid" when i unequip a pet

I tried playing animation on pets locally. I placed this local script in startercharacterscripts and it works well, but when unequip a pet it spams this error
LoadAnimation requires the Humanoid object (Angelus_Breku's Pet.Humanoid) to be a descendant of the game object
I imagine it will cause lag on client side so what do i do?
When i equip a pet it gets cloned from ReplicatedStorage into player’s character and when i unequip it gets destroyed.
I tried changing the ways the functions gets triggered but that seems not to be the problem, is there a way i can stop the PlayAnimation function from running when i unequip a pet?

local player = game:GetService("Players").LocalPlayer
local HumanCharacter = script.Parent
local human = HumanCharacter:WaitForChild("Humanoid")

local function playAnimationFromServer(character, animation)
	local humanoid = character:WaitForChild("Humanoid")
	if humanoid then
		local animator = humanoid:WaitForChild("Animator")
		if animator then
			local animationTrack = animator:LoadAnimation(animation)
			animationTrack:Play()
			print(animation)
			return animationTrack
		end
	end
end

HumanCharacter.ChildAdded:Connect(function(child)
	local PetCharacter = HumanCharacter:FindFirstChild(player.Name.."'s Pet")
	if PetCharacter then
		human.Running:Connect(function(speed)
			playAnimationFromServer(PetCharacter,PetCharacter.Animation.Walking)
		end)
		human.Jumping:Connect(function(isActive)
			playAnimationFromServer(PetCharacter,PetCharacter.Animation.Jumping)
		end)
		human:GetPropertyChangedSignal("MoveDirection"):Connect(function()
			local State = human:GetState()
			if human.MoveDirection.Magnitude < 0.75 and State ~= Enum.HumanoidStateType.Jumping and State ~= Enum.HumanoidStateType.Freefall then
				playAnimationFromServer(PetCharacter,PetCharacter.Animation.Idle)
			end
		end)
	end
end)

Does the console show any line number in the error?

10th line, the line with LoadAnimationㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

I’ll just suppress the error since i found no other solution.

You could try checking if the pet exists in the player before running the function and if it returns nil then won’t run the function

You’re trying to load an animation into something that doesn’t exist in workspace, so do the above and it should work fine