I need help with a script!

Hi there!

If you don’t know me yet, I like to go by local!

I made a script that is supposed to remove the player’s head accesories and then add a new one with the ID listed at the end of the script. It works just fine but it does not remove the player’s head accesories.

The script:

local function removeHeadAccessories(player)
	local character = player.Character or player.CharacterAdded:Wait()
	for _, accessory in ipairs(character:GetChildren()) do
		if accessory:IsA('Accessory') then
			local handle = accessory:FindFirstChild('Handle')
			if handle and handle:IsA('Part') and handle:FindFirstChild('Mesh') then
				local attachment = handle:FindFirstChildWhichIsA('Attachment')
				if attachment and attachment.Parent == character.Head then
					accessory:Destroy()
				end
			end
		end
	end
end

local function changeHeadAccessory(player, accessoryId)
	local character = player.Character or player.CharacterAdded:Wait()
	local head = character:FindFirstChild('Head')
	if head then
		local assetId = "rbxassetid://" .. accessoryId
		local accessory = game:GetService("InsertService"):LoadAsset(accessoryId):GetChildren()[1]
		accessory.Parent = game.Workspace
		character.Humanoid:AddAccessory(accessory)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		removeHeadAccessories(player)
		changeHeadAccessory(player, 13035709663) -- Use the numeric ID here
	end)
end)