--------------------------------------------------------------------------------


local function FormatValue(Value: any): string
	return typeof(Value) == "number" and FormatNumber(Value, "Suffix") or tostring(Value)
end

local function OnPlayerAdded(Player: Player)
	local pData = PlayerData:WaitForChild(Player.UserId, 5)
	local Stats = pData and pData:WaitForChild("Stats", 5)
	if not Stats then return end
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	for _, StatName in StatsToShow do
		local Stat = Stats:WaitForChild(StatName, 5)
		
		if Stat then
			local ValueObject = Instance.new("StringValue")
			ValueObject.Name = StatName
			ValueObject.Value = FormatValue(Stat.Value)
			ValueObject.Parent = leaderstats
			
			Stat.Changed:Connect(function()
				ValueObject.Value = FormatValue(Stat.Value)
			end)
		end
	end
	
	leaderstats.Parent = Player
end

Players.PlayerAdded:Connect(OnPlayerAdded)
for _, Player in Players:GetPlayers() do
	task.defer(OnPlayerAdded, Player)
end

return {}
