Why doesn't it delete the hat?

This script is supposed to add a hat to the character and once the player reaches a certain amount of points it destroys the previous hat and adds the new one. The problem comes when it is supposed to change hats, it will add the new hat but the old one doesn’t get destroyed.

local HatFolder = game.ReplicatedStorage.HatFolder
local Hats = {
	{0, "Pal hair"},
	{25, "Shaggy"},
	{50, "Chillcap"},
}
local function AddHat(Character, Hat)
	wait(1)

	local Humanoid = Character.Humanoid
	local HatPhys = HatFolder:FindFirstChild(Hat):Clone()

	for _, V in pairs(Character:GetChildren()) do
		if V:IsA("Accessory") then
			V:Destroy()
		end
	end    

	Humanoid:AddAccessory(HatPhys)
end

local function HatCheck(Value, Plr)
	local BiggestHat

	for _,V in pairs(Hats) do
		if Value >= V[1] then
			BiggestHat = V[2]
		end
	end
   AddHat(Plr.Character, BiggestHat)
end

This might seem like a dumb question but are you sure the hats are accessories, not something similar (like one of the classname Hat)?

Also have you tried printing V?

Yep, they’re all hats, I’m really not sure what’s causing it to not destroy the hat.

Maybe I’m misunderstanding but they’re hats, not accessories? You should probably switch them over to accessories then:

Screen Shot 2021-10-16 at 11.38.24 PM

2 Likes

ohhh, sorry I completely read that wrong, I’ll change them and see.

2 Likes

Accessories, Hats, and Accroutements are all instances which are used for avatar items so you should try use all three.

1 Like

Hats are deprecated, don’t use them. The other 2 should work though, but accessories are probably the best use for this case though.

3 Likes

Yea accessories and accroutements should work.

1 Like

Thank you, that seemed to work!

1 Like