Hat remover doesn't work when player spawns

Recently, I have been trying to make a clothes/accessory spawner when a player joins a specific team and there character has loaded. It adds the clothes and accessories fine just… the hat remover doesn’t work for some reason. Any and all help will be appreciated, so thank you in advance! :smile:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local folder = script:FindFirstChild(player.Team.Name)
		if folder then
			local tools = folder:FindFirstChild("Tools")
			if tools then
				for i,v in pairs(tools:GetChildren()) do
					v:Clone().Parent = player.Backpack
				end
			end
			
			local clothes = folder:FindFirstChild("Clothes")
			if clothes then
				for i,v in pairs(clothes:GetChildren()) do
					local clothing = character:FindFirstChild(v.Name)
					local db = true
					if clothing then
						clothing:Destroy()
					end
					v:Clone().Parent = character
				end
			end
			
			local accessories = folder:FindFirstChild("Accessories")
			if accessories then
				for i,v in pairs(accessories:GetChildren()) do
					if v.Name == "Hat" then
						for _,eaccessories in pairs(character:GetChildren()) do
							print(eaccessories)
						if eaccessories:IsA("Accessory") then
								print(eaccessories)
								eaccessories:Destroy()
						end
							local g = v:Clone()
							g.Parent = character
						local C = g:GetChildren()
						if g then
							for i=1, #C do
			if C[i].className == "Part" then
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
				g.Middle.Transparency = 1
			end
				local Y = Instance.new("Weld")
				Y.Part0 = character.Head
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			h[i].Anchored = false
			h[i].CanCollide = false
		end
							end
						end
					end
				end
			end
		end
	end)
end)

--//Hat Remover\\--
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local d = character:GetChildren()
		for i = 1,#d do
			print(d[i])
			if d[i]:IsA("Accessory") then
				d[i]:Destroy()
			end
		end
	end)
end)

Its because it is still at the table, therefore it will still clone it (I think its that actually)

The table would be the :GetChildren()

I recall CharacterAdded having some odd behavior in my own project, where the parts would become outdated after a moment or so (meaning when I welded a part to another, the welded part would disappear).
You could add a wait(1), to fix it for now, even though that’s a slightly cheap solution.

3 Likes

Ahh yes, that worked thank you, I see the accessories load in after the actual character, strange, seems like a bug.

1 Like

You should use player.CharacterAppearanceLoaded:Connect(function()) instead. The wait() could still error if their client is slow, pretty sure.

The character appearance is loaded after the character spawns. Particularly when it comes to Accessories, there’s no real guarantee as to when they’ll load onto the character. Waiting a second covers most cases, but there isn’t really a guarantee that it’ll cover all cases if the APIs responsible for retrieving the assets a user is wearing happen to be slow in that moment.

If you’re trying to detect assets being added to the character, the best way to go about this would be to listen to the .ChildAdded event on the character.

However, considering @op is trying to remove all clothing and accessories, wouldn’t it be simpler to just listen to Players.PlayerAdded and set all new players’ Player.CanLoadCharacterAppearance property to false? This will cause all players to join with a default character.

1 Like

Ah, that makes sense. Thanks for clarifying that.

The original poster is only attempting to remove the hats/accessories, not the entire outfit.
By enabling that, it’ll make all the characters the default which is just a gray character with a smiley face.