-- Don't edit if you don't know what you're doing --

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DataStore = DataStoreService:GetOrderedDataStore("f"..script.Parent.catalogdata.Value)


local Template = script.objTemplate

local COLORS = {
	Default = Color3.fromRGB(38, 50, 56),
	Gold = Color3.fromRGB(255, 215, 0),
	Silver = Color3.fromRGB(192, 192, 192),
	Bronze = Color3.fromRGB(205, 127, 50)
}
local ABBREVIATIONS = { "K", "M", "B", "T" }


local function toHumanReadableNumber(num)
	if num < 1000 then
		return tostring(num)
	end

	local digits = math.floor(math.log10(num)) + 1
	local index = math.min(#ABBREVIATIONS, math.floor((digits - 1) / 3))
	local front = num / math.pow(10, index * 3)

	return string.format("%i%s+", front, ABBREVIATIONS[index])
end

local function getItems()
	local data = DataStore:GetSortedAsync(false, maxItems, minValueDisplay, maxValueDisplay)
	local topPage = data:GetCurrentPage()
	
	for i,v in ipairs(game.Players:GetPlayers()) do
		pcall(function()
			v.PlayerGui.SurfaceGui.Frame.Contents.Items.Nothing.Visible = #topPage == 0 and true or false
		end)
	end
	

	for position, v in ipairs(topPage) do
		local userId = v.key
		local value = v.value
		local username = "?"
		local color = COLORS.Default
		local find = game.ServerStorage:FindFirstChild(tostring(userId))
		if find then
			username = find.Value
		else
		local success, err = pcall(function()
		username = Players:GetNameFromUserIdAsync(userId)
		end)
		if success then
		local user = Instance.new("StringValue")
		user.Name = userId
		user.Value = username
		user.Parent = game.ServerStorage.usernames
		else
			username = "?"
		end
		end
		
		local  f = script.ImageLabel:Clone()
		f.Image = "rbxthumb://type=AvatarHeadShot&id="..userId.."&w=420&h=420"
		
		pcall(function()
		
			if not game.ReplicatedStorage.pendingthumbnails:FindFirstChild(userId) then
				if not game.Players:FindFirstChild(username) then
					Instance.new("IntValue",game.ReplicatedStorage.pendingthumbnails).Name = userId
				end
			end
		
		end)
		
		local h = game.ReplicatedStorage.playerheadshots:FindFirstChild(username) 
		if h then
			h:Clone().Parent = f
			f.Image = ""
		end
		
		local icon = f

		if position == 1 then
			color = COLORS.Gold
		elseif position == 2 then
			color = COLORS.Silver
		elseif position == 3 then
			color = COLORS.Bronze
		end

		local item = Template:Clone()
		icon.Parent = item.Values.plricon
		item.Name = username
		item.LayoutOrder = position
		item.Values.Number.TextColor3 = color
		item.Values.Number.Text = position
		item.Values.Username.Text = username
		item.Values.Value.Text = abbreviateValue and toHumanReadableNumber(value) or value
		
		
		
		for i,v in ipairs(game.Players:GetPlayers()) do
			pcall(function()
				item:Clone().Parent = v.PlayerGui.SurfaceGui.Frame.Contents.Items
			end)
		end
		
		
	end
end


wait(5)

while true do
	print('run')
	for _, player in pairs(Players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")

		if not leaderstats then
			warn("Couldn't find leaderstats!")
			break
		end

		local statsValue = leaderstats:FindFirstChild(statsName)

		if not statsValue then
			warn("Couldn't find " .. statsName .. " in leaderstats!")
			break
		end
		
		pcall(function()
			DataStore:UpdateAsync(player.UserId, function()
				return tonumber(statsValue.Value)
			end)
		end)
	end
	
	for i,v in ipairs(game.Players:GetPlayers()) do
		pcall(function()
			for _, item in pairs(v.PlayerGui.SurfaceGui.Frame.Contents.Items:GetChildren()) do
				if item:IsA("Frame") then
					item:Destroy()
				end
			end
		end)
	end
	

	getItems()


	wait(updateEvery)
end