Problems with global leaderboard. please tell me how to fix this

my global leaderboard has been working fine for a long long time but suddenly it stopped working. I found out that it doesn’t record the name of the players anymore (though not always, I’ve seen that it does sometimes work only one time ‘stops after refresh’ at the start when I join the game). The scipt below here:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("YumsSave2")
local MainFrame = script.Parent.Parent
local RefreshRate = 4
local num = 0
local function RefreshLeaderboard()
	for i, Player in pairs(game.Players:GetChildren()) do
		DataStore:SetAsync(Player.UserId, Player.leaderstats.Yums.Value)
	end
	local success, Error = pcall(function()
		local success2, Error2 = pcall(function()
			local Data = DataStore:GetSortedAsync(false, 77)
			local CreditsPage = Data:GetCurrentPage()
		end)
		if Error2 then
			RefreshLeaderboard()
		end
		local Data = DataStore:GetSortedAsync(false, 75)
		local CreditsPage = Data:GetCurrentPage()
		for Rank, SavedData in ipairs(CreditsPage) do
			if tonumber(SavedData.key) >= 0 then
				local Username = game.Players:GetNameFromUserIdAsync(tonumber(SavedData.key)) --doesn't work (doesn't error btw)	
				local Credits = SavedData.value
				--warn(Username)
				if Credits then and not script.Parent:FindFirstChild(Username)
				local NewSample = script.Sample:Clone()
					for i,v in pairs(script.Parent.ScrollingFrame:GetChildren()) do
						if v.Name ~= 'UI' then
							num += 1
						end
					end
				NewSample.Visible = true
						NewSample.Parent = MainFrame.SurfaceGui.ScrollingFrame
				NewSample.Name = Username
				NewSample.PName.Text = Username
				if Credits <= 1000 then
					NewSample.Value.Text = Credits.." Yums"
				else
					if Credits >= 1000 and Credits <= 1000000 then
						local num = math.floor(Credits / 1000)
						local deci = math.floor(Credits - (num * 1000))
						NewSample.Value.Text = (num .."."..math.floor(deci / 100).."K Yums")
					else
						if Credits >= 1000000 and Credits <= 1000000000 then
							local num = math.floor(Credits / 1000000)
							local deci = math.floor(Credits - (num * 1000000))
							NewSample.Value.Text = (num .."."..math.floor(deci / 100000).."M Yums")
						else
							if Credits >= 1000000000 and Credits <= 1000000000000 then
								local num = math.floor(Credits / 1000000000)
								local deci = math.floor(Credits - (num * 1000000000))
								NewSample.Value.Text = (num .."."..math.floor(deci / 100000000).."B Yums")
							else
								if Credits >= 1000000000000 then
									local num = math.floor(Credits / 1000000000000)
									local deci = math.floor(Credits - (num * 1000000000000))
									NewSample.Value.Text = (num .."."..math.floor(deci / 100000000000).."T Yums")
								end
							end
						end
					end
					end
						
					NewSample.Rank.Text = "#"..num + 1
					num = 0
			else
			end
			else
			end
		end
	end)
end

local suc, err = pcall(function()
	RefreshLeaderboard() 		
end)
if err then
	print(err)
end

while true do
	for i, Frame in pairs(MainFrame.SurfaceGui.ScrollingFrame:GetChildren()) do
		if Frame.Name ~= "Sample" and Frame:IsA("Frame") then
			Frame:Destroy()
		end
	end
	RefreshLeaderboard()
	Rank2 = 1
	wait(RefreshRate)
end

I doubt about this line. while true do() actually lag things, just add some wait()

ok didn’t know about that btw (used both of them). I will do that and I’II hope that fixes the issue. thanks for your help! if it fixes it I will give you the solution.

1 Like

@Moneypro456789 I used while wait(0.1) do but doesn’t fix it. the real issue probably lies elsewhere.

I think not 0.1, I also ran in this problem, take like 5 seconds or 10 seconds.

Basically, this is a function with alot of lines of code, so 0.1 is pretty small amount. Just like DataStores, it takes time, and it also have the use of DataStores, so use like 10 or 30 seconds maximum.

Yes, but I did change some stuff. I’II try while wait(5) do.

@Moneypro456789 it only works one time (doesn’t refresh). its kinda strange because it did work before but now it just doesn’t get the player name.

Have you tried doing like 30 seconds ? Also, why there are 2 pcall functions ? I don’t there is a need of 2 pcalls.

ok, I’II try that too. you said you also follow GamerM8’s video did you also fix the same issue? @Moneypro456789

I didn’t faced issues actually, I removed alot of unneccessary steps and I knew DataStores takes alot of time, so I gave it a time of like 30 seconds. I also tried doing like 5 seconds, but it can’t work. Basically what happens is it tries to run the code it can in 5 seconds but as it couldn’t load the names, you don’t see the names. Code just goes linewise.

Hey @Mrtgr33 , did it got fixed ?

like you said it works now but only loads the data up to 30, 45 players and I have a top 75. is there any way to make it take less time to run the code? @Moneypro456789

I would suggest you to remove some of the pcalls. It isn’t much useful. It can also reduce your time. Remove some unnessecasary code (if any) or just increase the time.

okay I’II do that. thanks for the help

1 Like

Also, if it helps, mark it as a solution please, actually I’m trying to solve problems nowadays lol :upside_down_face:

does this code also costs a lot of time? can’t delete this btw bc I need this.

	if Credits <= 1000 then
					NewSample.Value.Text = Credits.." Yums"
				else
					if Credits >= 1000 and Credits <= 1000000 then
						local num = math.floor(Credits / 1000)
						local deci = math.floor(Credits - (num * 1000))
						NewSample.Value.Text = (num .."."..math.floor(deci / 100).."K Yums")
					else
						if Credits >= 1000000 and Credits <= 1000000000 then
							local num = math.floor(Credits / 1000000)
							local deci = math.floor(Credits - (num * 1000000))
							NewSample.Value.Text = (num .."."..math.floor(deci / 100000).."M Yums")
						else
							if Credits >= 1000000000 and Credits <= 1000000000000 then
								local num = math.floor(Credits / 1000000000)
								local deci = math.floor(Credits - (num * 1000000000))
								NewSample.Value.Text = (num .."."..math.floor(deci / 100000000).."B Yums")
							else
								if Credits >= 1000000000000 then
									local num = math.floor(Credits / 1000000000000)
									local deci = math.floor(Credits - (num * 1000000000000))
									NewSample.Value.Text = (num .."."..math.floor(deci / 100000000000).."T Yums")
								end
							end
						end
					end
					end

So you need abbrievations right ? I have a code which costs hardly 3 lines to short up numbers.

its part of my script. this is the part I didn’t get from the tutorial.

1 Like

yes → not part of my answer (because otherwise the message is too short)

yes I’II do that if my issue is fixed :wink: