Whats wrong with my leaderboard?

Hello, I’m trying to create a leaderboard but the leaderboard script stops going further than line 45 where I get the name of the player so I can create a template frame for the leaderboard. I have no clue why it happens. One of my other leaderboards that runs over the exact same script works fine but this one just stops running.Hope u guys can give some feedback that might can help me. Here is the script:

local ds = game:GetService("DataStoreService")

local coinsODS = ds:GetOrderedDataStore("eS")


local tfm = require(game.ReplicatedStorage:WaitForChild("TFM"))

local timeUntilReset = 30


local Short = require(game.ReplicatedStorage:WaitForChild("Short"))

while wait(1) do


	timeUntilReset = timeUntilReset - 1


	if timeUntilReset == 0 then

		timeUntilReset = 30


		for i, plr in pairs(game.Players:GetPlayers()) do
			
			coinsODS:SetAsync(plr.UserId, plr.Stats.TimePlayed.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 = coinsODS:GetSortedAsync(false, 100)
			local coinsPage = data:GetCurrentPage()

			for rankInLB, dataStored in ipairs(coinsPage) do
				

				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 = tfm:Convert(coins, "Short", false)

				template.Parent = script.Parent			
			
				if rankInLB == 1 then
					game.Workspace.NpcTimePlayed.UserId.Value = userid
				end
			end			
		end)
	end
end

I think you’re getting rate limited since you’re using GetNameFromUserIdAsync way too much

I used it in a loop a long time ago to see available usernames, then lost access to roblox requests like Publishing a game or more GetNameFromUserIdAsync for like an hour

1 Like

I think @4SHN is right, running this once a second and looping through your data would definitely get you throttled by Roblox.

A potential fix would be to store the Player’s username in PlayerData and then pull directly from your DataStore, rather than looking up their User based on their PlayerID.

You can update the PlayerName whenever they join, that way it stays up to date if they change it.

1 Like

Here are some bad practices I see:

  • Using game.Service Name instead of game:GetService().
  • Saving data for possibly multiple players every 30 seconds. It should be more around 180 seconds.
  • You aren’t using Pcalls correctly. I suggest not using them at all as they are expensive.
  • You’re using GetName and GetUserId too much.
1 Like