Help With Global Leaderboard

I am trying to make a global leaderboard and when I play nothing comes up on the board. Can someone see if I did something wrong.

local ds = game:GetService("DataStoreService")

local TopRaisedODS = ds:GetOrderedDataStore("TopRaisedStats")

local timeUntilReset = 2
local nosaveplayer = {""}

while wait(1) do


	timeUntilReset = timeUntilReset - 1


	if timeUntilReset == 0 then

		timeUntilReset = 10


		for i, plr in pairs(game.Players:GetPlayers()) do
			if table.find(nosaveplayer,plr.Name) then
			else
				TopRaisedODS:SetAsync(plr.UserId, plr.leaderstats:WaitForChild("Raised", 10).Value)
			end

		end

		for i, leaderboardRank in pairs(script.Parent:GetChildren()) do

			if leaderboardRank.ClassName == "Frame" then
				leaderboardRank:Destroy()
			end
		end


		local success, errorMsg = pcall(function()

			local data = TopRaisedODS:GetSortedAsync(false, 50)
			local TopRaisedPage = data:GetCurrentPage()

			for rankInLB, dataStored in ipairs(TopRaisedPage) do

				local name
				local userid

				local success, errormsg = pcall(function()
					name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
					userid = dataStored.key
				end)
				if not success then
					name = "Terminated User!"
					userid = 50
				end
				local RobuxRaised = dataStored.value


				local template = script.Template:Clone()

				--template.Name = name .. "Leaderboard"

				template.Info.Username.Text = "@" .. name

				template.Info.Avatar.Image = "rbxthumb://type=AvatarHeadShot&id="..userid.."&w=150&h=150" 

				template.Info.Rank.Text = rankInLB

				template.Stats.Raised.Text = require(game.ReplicatedStorage.CommaCreator).en(RobuxRaised)

				template.Parent = script.Parent

				if rankInLB == 100 then

					local NPC = workspace:WaitForChild("NPC").RaisedNPC
					local success, errormsg = pcall(function()
						local HC = game.Players:GetHumanoidDescriptionFromUserId(userid) 

						NPC.MythzRB.Humanoid:RemoveAccessories()
						NPC.MythzRB.Humanoid:ApplyDescription(game.Players:GetHumanoidDescriptionFromUserId(1))
						wait(0.1)
						NPC.MythzRB.Humanoid:ApplyDescription(HC)
					end)
					if not success then
						warn(errormsg)
						NPC.MythzRB.Humanoid:ApplyDescription(game.Players:GetHumanoidDescriptionFromUserId(1))
					end

				end
			end			
		end)
	end
end

Location of script
Screenshot 2023-08-17 at 21.40.09

If needed here is the model

1 Like
local StatsName = "Raised" -- Your stats name
local MaxItems = 100 -- Max number of items to be displayed on the leaderboard
local MinValueDisplay = 0 -- Any numbers lower than this will be excluded
local MaxValueDisplay = 10e15 -- (10 ^ 15) Any numbers higher than this will be excluded
local UpdateEvery = 30 -- (in seconds) How often the leaderboard has to update

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()
	
	
	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()
	List.ListContent.GuideTopBar.Value.Text = StatsName
	List.CanvasSize = UDim2.new(0, 0, 0, ItemsFrame.UIListLayout.AbsoluteContentSize.Y + 35)
	wait(UpdateEvery)
end

when utilising for loops, it’s more efficient using _, v than i, v if you will not mention i

wait() is deprecated. use task.wait()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.