-- Main function that will be called when a player joins the game

local function PlayerJoined(player)
	-- Secondary function to remove unwanted meshes from the player's character
	local function RemoveMeshes(character)
		-- Wait for the humanoid to load
		local humanoid = character:WaitForChild("Humanoid")

		-- Wait for a short time to ensure all accessories are loaded properly
		task.wait()

		-- Modify the description of the humanoid to reset its appearance
		local currentDescription = humanoid:GetAppliedDescription()
		currentDescription.Head = 0
		currentDescription.Torso = 0
		currentDescription.LeftArm = 0
		currentDescription.RightArm = 0
		currentDescription.LeftLeg = 0
		currentDescription.RightLeg = 0
		humanoid:ApplyDescription(currentDescription)

		-- Remove any unwanted accessories that are not attached to the character's head
		for _, accessory in pairs(character:GetChildren()) do
			if accessory:IsA("Accessory") then
				local handle = accessory:FindFirstChild("Handle")
				if handle then
					local weld = FindChildOfClassRecursive(accessory, "Weld") -- Use the recursive search function here
					if weld then
					end
					if weld and weld.Part1.Name ~= character:FindFirstChild("Head").Name then
						accessory:Destroy()
					end
				end
			end
		end
	end

	-- Connect the RemoveMeshes function to the CharacterAppearanceLoaded event of the player
	player.CharacterAppearanceLoaded:Connect(RemoveMeshes)
end
