local i=game:GetService("\068\097\116\097\083\116\111\114\101\083\101\114\118\105\099\101")local c=i:GetOrderedDataStore("\071\108\111\098\097\108\076\101\097\100\101\114\098\111\097\114\100\095"..StatsName)local K=script.Parent local J=script.Sample local Q=K.Frame.List local o=Q.ListContent.Items local function D()local i=c:GetSortedAsync(false,MaxItems,MinValueDisplay,MaxValueDisplay)local K=i:GetCurrentPage()o.Nothing.Visible=#K==0 and true or false for i,c in ipairs(K)do local K=c.key local Q=c.value local D="\091\078\111\116 \065\118\097\105\108\097\098\108\101\093"local g=Color3.fromRGB(38,50,56)local B,X=pcall(function()D=game.Players:GetNameFromUserIdAsync(K)end)if i==1 then g=Color3.fromRGB(255,215,0)elseif i==2 then g=Color3.fromRGB(192,192,192)elseif i==3 then g=Color3.fromRGB(205,127,50)end local V=J:Clone()V.Name=D V.LayoutOrder=i V.Values.Number.TextColor3=g V.Values.Number.Text=i V.Values.Username.Text=D V.Values.Value.Text=Q V.Parent=o end end while true do for i,K in pairs(game.Players:GetPlayers())do local J=(K.leaderstats:WaitForChild(StatsName)).Value if J then pcall(function()c:UpdateAsync(K.UserId,function(i)return tonumber(J)end)end)end end for i,c in pairs(o:GetChildren())do if c:IsA("\073\109\097\103\101\076\097\098\101\108")then c:Destroy()end end D()wait()K.Heading.Heading.Text=StatsName.." \076\101\097\100\101\114\098\111\097\114\100"Q.ListContent.GuideTopBar.Value.Text=StatsName Q.CanvasSize=UDim2.new(0,0,0,o.UIListLayout.AbsoluteContentSize.Y+35)wait(UpdateEvery)end


local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("GlobalLeaderboard_" .. StatsName)
local SurfaceGui = script.Parent
local Sample = script.Sample
local List = SurfaceGui.Frame.List
local ItemsFrame = List.ListContent.Items

local function GetItems()
	local Data = DataStore:GetSortedAsync(false, MaxItems, MinValueDisplay, MaxValueDisplay)
	local TopPage = Data:GetCurrentPage()
	
	ItemsFrame.Nothing.Visible = #TopPage == 0 and true or false
	
	for i, v in ipairs(TopPage) do
		local UserId = v.key
		local Value = v.value
		local Username = "[Not Available]"
		local Color = Color3.fromRGB(38, 50, 56)
		
		local Success, Error = pcall(function()
		 	Username = game.Players:GetNameFromUserIdAsync(UserId)
		end)
		
		if i == 1 then
			Color = Color3.fromRGB(255, 215, 0)
		elseif i == 2 then
			Color = Color3.fromRGB(192, 192, 192)
		elseif i == 3 then
			Color = Color3.fromRGB(205, 127, 50)
		end
		
		local Item = Sample:Clone()
		Item.Name = Username
		Item.LayoutOrder = i
		Item.Values.Number.TextColor3 = Color
		Item.Values.Number.Text = i
		Item.Values.Username.Text = Username
		Item.Values.Value.Text = Value
		Item.Parent = ItemsFrame
	end
end

while true do
	for i, v in pairs(game.Players:GetPlayers()) do
		local Stats = v.leaderstats:WaitForChild(StatsName).Value
		if Stats then
			pcall(function()
				DataStore:UpdateAsync(v.UserId, function(Value)
					return tonumber(Stats)
				end)
			end)
		end
	end
	
	for i, v in pairs(ItemsFrame:GetChildren()) do
		if v:IsA("ImageLabel") then
			v:Destroy()
		end
	end
	
	GetItems()
	
	wait()
	SurfaceGui.Heading.Heading.Text = StatsName .. " Leaderboard"
	List.ListContent.GuideTopBar.Value.Text = StatsName
	List.CanvasSize = UDim2.new(0, 0, 0, ItemsFrame.UIListLayout.AbsoluteContentSize.Y + 35)
	wait(UpdateEvery)
end
