local e=game:GetService("\068\097\116\097\083\116\111\114\101\083\101\114\118\105\099\101")local c=game:GetService("\080\108\097\121\101\114\115")local D=e:GetOrderedDataStore("\102"..script.Parent.catalogdata.Value)local U=script.objTemplate local W={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 K={"\075","\077","\066";"\084"}local function z(e)if e<1000 then return tostring(e)end local c=math.floor(math.log10(e))+1 local D=math.min(#K,math.floor((c-1)/3))local U=e/math.pow(10,D*3)return string.format("\037\105\037\115\043",U,K[D])end local function p()local e=D:GetSortedAsync(false,maxItems,minValueDisplay,maxValueDisplay)local K=e:GetCurrentPage()for e,c in ipairs(game.Players:GetPlayers())do pcall(function()c.PlayerGui.SurfaceGui.Frame.Contents.Items.Nothing.Visible=#K==0 and true or false end)end for e,D in ipairs(K)do local K=D.key local p=D.value local H="\063"local Q=W.Default local E=game.ServerStorage:FindFirstChild(tostring(K))if E then H=E.Value else local e,D=pcall(function()H=c:GetNameFromUserIdAsync(K)end)if e then local e=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")e.Name=K e.Value=H e.Parent=game.ServerStorage.usernames else H="\063"end end local P=script.ImageLabel:Clone()P.Image="\114\098\120\116\104\117\109\098\058\047\047\116\121\112\101\061\065\118\097\116\097\114\072\101\097\100\083\104\111\116\038\105\100\061"..(K.."\038\119\061\052\050\048\038\104\061\052\050\048")pcall(function()if not game.ReplicatedStorage.pendingthumbnails:FindFirstChild(K)then if not game.Players:FindFirstChild(H)then(Instance.new("\073\110\116\086\097\108\117\101",game.ReplicatedStorage.pendingthumbnails)).Name=K end end end)local h=game.ReplicatedStorage.playerheadshots:FindFirstChild(H)if h then(h:Clone()).Parent=P P.Image=""end local r=P if e==1 then Q=W.Gold elseif e==2 then Q=W.Silver elseif e==3 then Q=W.Bronze end local l=U:Clone()r.Parent=l.Values.plricon l.Name=H l.LayoutOrder=e l.Values.Number.TextColor3=Q l.Values.Number.Text=e l.Values.Username.Text=H l.Values.Value.Text=abbreviateValue and z(p)or p for e,c in ipairs(game.Players:GetPlayers())do pcall(function()(l:Clone()).Parent=c.PlayerGui.SurfaceGui.Frame.Contents.Items end)end end end wait(5)while true do print("\114\117\110")for e,c in pairs(c:GetPlayers())do local U=c:FindFirstChild("\108\101\097\100\101\114\115\116\097\116\115")if not U then warn("\067\111\117\108\100\110\'\116 \102\105\110\100 \108\101\097\100\101\114\115\116\097\116\115\033")break end local W=U:FindFirstChild(statsName)if not W then warn("\067\111\117\108\100\110\'\116 \102\105\110\100 "..(statsName.." \105\110 \108\101\097\100\101\114\115\116\097\116\115\033"))break end pcall(function()D:UpdateAsync(c.UserId,function()return tonumber(W.Value)end)end)end for e,c in ipairs(game.Players:GetPlayers())do pcall(function()for e,c in pairs(c.PlayerGui.SurfaceGui.Frame.Contents.Items:GetChildren())do if c:IsA("\070\114\097\109\101")then c:Destroy()end end end)end p()wait(updateEvery)end


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
