Global leaderboard stopped updating or showing correct top 50 players

I’ve been using some free modeled global leaderboard for a while, and it worked fine at first when the game didn’t have many players, however as more people have played, it seems to get buggy, and doesn’t always update/show the correct top 50 players.

Here’s the script:

local ds = game:GetService("DataStoreService")
local ODS = ds:GetOrderedDataStore("StrengthLb")

function comma(amount)
	local formatted = amount
	while true do  
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
		if (k==0) then
			break
		end
	end
	return formatted
end

local function formatnum(number)
	local suffixes = {"", "K", "M", "B", "T","Qa","Qi"} 
	local suffixIndex = 1

	while number >= 1000 and suffixIndex < #suffixes do
		number = number / 1000
		suffixIndex = suffixIndex + 1
	end

	number = math.floor(number * 100 + 0.5) / 100

	return tostring(number) .. suffixes[suffixIndex]
end

local function updateLeaderboard()
	for i, plr in pairs(game.Players:GetPlayers()) do
		ODS:SetAsync(plr.UserId, math.round(plr.leaderstats.Strength.Value))
	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 = ODS:GetSortedAsync(false, 50)
		local coinsPage = data:GetCurrentPage()

		for rankInLB, dataStored in ipairs(coinsPage) do
			if rankInLB <= 50 then -- show top 50 players
				local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
				local userid = game.Players:GetUserIdFromNameAsync(name)
				local coins = dataStored.value

				local template = script.Template:Clone()
				template.Name = name .. "Leaderboard"
				template.PlrName.Text = name
				template.Rank.Text = "#" .. rankInLB
				template.Coins.Text = formatnum(coins)
				template.Parent = script.Parent
				template.Thumbnail.Image = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
			end
		end
	end)
end


while task.wait(60) do

	updateLeaderboard()
end

There are no errors or anything in the output.

Any help would be appreciated!

1 Like

Theres no errors because its surrounded in a pcall function…

After the pcall function add a way of viewing the errors that are occuring:

if not success then
	warn(errorMsg)
end
1 Like

The only concerning thing that i found in your code is that you don’t have a name cache

When using :GetNameFromUserIdAsync() you are making an API call to the website, and you can get request limited from making too many of them. This is why its recommended making a table of names that you have already called with this function.
The documentation supports this practice aswell.

local Players = game:GetService("Players")

-- Create a table called 'cache' to store each 'Name' as they are found.
-- If we lookup a 'Name' using the same 'UserId', the 'Name' will come
-- from cache (fast) instead of GetNameFromUserIdAsync() (yields).
local cache = {}

function getNameFromUserId(userId)
	-- First, check if the cache contains 'userId'
	local nameFromCache = cache[userId]
	if nameFromCache then
		-- if a value was stored in the cache at key 'userId', then this 'nameFromCache'
		-- is the correct Name and we can return it.
		return nameFromCache
	end

	-- If here, 'userId' was not previously looked up and does not exist in the
	-- cache. Now we need to use GetNameFromUserIdAsync() to look up the name
	local name
	local success, _ = pcall(function()
		name = Players:GetNameFromUserIdAsync(userId)
	end)
	if success then
		-- if 'success' is true, GetNameFromUserIdAsync() successfully found the
		-- name. Store this name in the cache using 'userId' as the key so we
		-- never have to look this name up in the future. Then return name.
		cache[userId] = name
		return name
	end
	-- If here, 'success' was false, meaning GetNameFromUserIdAsync()
	-- was unable to find the 'name' for the 'userId' provided. Warn the user
	-- this happened and then return nothing, or nil.
	warn("Unable to find Name for UserId:", userId)
	return nil
end
2 Likes

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