Why isn't the hat getting destroyed

Hello, I’ve been having some troubles trying to get this script to work, it is supposed to destroy the hat that the player has once they get a certain amount of points but it isn’t doing that. I’m really not sure what the problem is as it doesn’t give me any errors in the output.

local Players = game:GetService("Players")
local function PlayerAdded(player)
	local function check(value)
		if value >= 50 and player.Character:FindFirstChild("Shaggy") then
			player.Character.Shaggy:Destroy()
		
		end
	end
	player.leaderstats.Robux.Changed:Connect(check)
	player.leaderstats.Robux = 50
	player.CharacterAppearanceLoaded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local accessories = {}
		humanoid:RemoveAccessories()
		wait(5)


		if player and player.Character and player.Character == character then
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			if humanoid:GetState() ~= Enum.HumanoidStateType.Dead then

				for _, accessory in pairs(accessories) do 
					humanoid:AddAccessory(accessory)
				end
			end
		end

		accessories = nil
	end)
end
for _, player in pairs(Players:GetPlayers()) do 
	playerAdded(player)
end

You’re misusing the player added event.

You should just connect to it as such, instead of iterating over each player inside Players

local function PlayerAdded(plr)
  print("Hello", plr.Name)
end

Players.PlayerAdded:Connect(PlayerAdded)

He is iterating as that is the proper way to set it up, delays can cause this event not to fire, and the usage of WaitForChild in cases.
Here’s my method, which is very decent.

local GetPlayers = Players:GetPlayers()
for i = 1, (#GetPlayers)
do
	task.spawn(PlayerAdded, GetPlayers[i])
end
Players.PlayerAdded:Connect(PlayerAdded)

Read the full block of code. Its very clear they’re not connecting the event.

The code you sent is unnecessary since there is no yield, and Roblox will start a server script before PlayerAdded fires.

Yes, there are definitely places where this needs to be done (cough cough) chatservice, but here. Nah.

To the OP: You have a race condition with the leaderstats, you may want to add this line of code:

local leaderstats = player:WaitForChild("leaderstats")

Read the full block of code. Its very clear they’re not connecting the event.

I replied to you, not him.
I’m already aware of that as you already told him that.

The code you sent is unnecessary since there is no yield, and Roblox will start a server script before PlayerAdded fires.

The code is not unnecessary, what it does is looping through players who did not pass the PlayerAdded event if there were any delays when the script ran (can be caused by WaitForChild and server delay in general).
Better to be safe than sorry, as a lot of players has experienced this already.