Weird bug with StarterCharacter appearance script

Hello Fellow Developers!

I am currently making a script for when a character is added, it sets the humanoid description to the players. This is because my game uses a startercharacter so objects can be added to the character and have everyone have it, but have the player still have their avatar items and accesories, etc.

This is my script:

local Players = game:GetService("Players")


local function clearCharacterAccessories(character)
	for _, v in character:GetDescendants() do
		if v:IsA("Accessory") or v:IsA("Clothing") or v:IsA("ShirtGraphic") then
			v:Destroy()
		end
	end
end

local function applyAvatarData(character, player)
	local humanoid = character:WaitForChild("Humanoid", 10)
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart", 10)

	if humanoid and humanoidRootPart then
		clearCharacterAccessories(character)

		local success, description = pcall(function()
			return Players:GetHumanoidDescriptionFromUserId(player.UserId)
		end)

		if success and description then
			task.wait(1)
			humanoid:ApplyDescription(description)
		else
			warn("Failed to fetch humanoid description for player: " .. player.Name)
		end
	else
		warn("Humanoid or HumanoidRootPart not found for player: " .. player.Name)
	end
end

local function onCharacterAdded(character, player)
	applyAvatarData(character, player)
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		onCharacterAdded(character, player)
	end)
end)

For some reason, this works perfectly fine when you first load in.
But when you reset, die, etc, this bug appears:
Screenshot 2025-01-15 at 4.29.49 PM
and this one too:
Screenshot 2025-01-15 at 4.30.05 PM

They both return this error 10+ times in quick succession, and I’ve tried everything to fix it, but nothing seems to work.

If anyone could help me, that would be very appreciated!

1 Like