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