I’m making a game where you get different hats when you reach a certain amount of points but I’m having some trouble. When you get to the number of points you need to get the next hat it adds the next hat but doesn’t destroy the previous one and I can’t figure out why.
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
game.Players.PlayerAdded:Connect(function(Plr)
Plr.CharacterAdded:Wait()
local Leaderstats = Plr:WaitForChild("leaderstats")
local RobuxValue = Leaderstats:WaitForChild("Robux")
local Connection
if RobuxValue then
HatCheck(RobuxValue.Value, Plr)
Connection = RobuxValue.Changed:Connect(function()
HatCheck(RobuxValue.Value, Plr)
end)
end
game.Players.PlayerRemoving:Connect(function(Plr2)
if Plr == Plr2 then
Connection:Disconnect()
end
end)
end)