Global Leaderboard not working?

Hello Developers,

Before I start, please note that I want to be explained the answer, not just spoonfed. I want to actually learn from this mistake. Please keep that in mind. :slightly_smiling_face:


I’ve been working on a Global Leaderboard for my game, K-Pop hangout. After watching a tutorial from @UseCode_Tap, I thought that I understood OrderedDataStores. However, this is the error I keep getting:

I’ve tried a few diffrent solutions with no luck. Here is my code:

DataService
local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetOrderedDataStore("timeDataStore".."-time")

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = WinsLeaderboard:GetSortedAsync(false, 5)
		local WinsPage = Data:GetCurrentPage()
		for Rank, data in ipairs(WinsPage) do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local Time = data.value
			local isOnLeaderboard = false
			for i, v in pairs (game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end
			local userName = Data.key
			print(userName)
			if Time and isOnLeaderboard == false then
				
				local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
				newLbFrame.Player.Text = Name
				newLbFrame.Time.Text = Time
				newLbFrame.Rank.Text = "#"..Rank
				newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + .08 * #game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren(), 0)
				newLbFrame.Parent = game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder
			end
			
			
		end
		
		
	end)
	
	if not success then
		warn(errorMessage)
	end
	
	
end

while true do
	
	for _, player in pairs (game.Players:GetPlayers()) do
		WinsLeaderboard:SetAsync(player.UserId .. "-time", player.leaderstats.TimePlayed.Value)
	end
	
	for _, frame in pairs (game.Workspace.GlobalLeaderboard.LeaderboardGUI.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	print("Updated Leaderboard")
	
	

	
	wait(30)
	


	
end


Here is workspace, just in case:

Data saves fine, datastores are seeminly not the issue. I think I might be using OrderedDataStore wrong.


Again, I am a novice scripter, so I would like to be explained the solution, not just spoonfed.

Thank you! :slightly_smiling_face:

2 Likes

did you mean to put an = between these? It’s saying there’s a problem with WinsPage in your ipairs loop so this is likely the issue :slight_smile:.

2 Likes

That fixed the original error, now I am getting this:

(Cannot believe I overlooked that!)

1 Like

When you click on it, does it redirect you to one of the scripts and the line the error has occurred on? Thank you, that would help :slight_smile:

2 Likes

Line 36 of LeaderboardHandler, specifically:

	if not success then
		warn(errorMessage)
	end

OK, this may be the line that’s causing the problem - maybe, I’m not an expert at DataStores. Try removing the “-time” bit at the end, I think that if you include that, it’s trying to define a DataStore called “timeDataStore-time”, so this could be the problem that’s then causing it to not work.

1 Like

-time is the specific key I am using, it’s better to have one datastore with different keys. (Correct me if I’m wrong about that).

1 Like

Another typo? Did you mean game.Workspace.GlobalLeaderboard?

2 Likes

You’re probably right, I usually make a separate datastore for everything because I’m :sparkles: overly complicating :sparkles:

1 Like

Still didn’t fix it, however probably fixed another error somewhere along the lines.

I need to be more careful! :man_facepalming:

1 Like

OK, just been reading through the developer pages on OrderedDataStores.

Here, try it just with:

local userName = data.key
print(userName)

See what it comes up with - if it doesn’t print anything at all, then something before the ipairs loop has gone wrong.

2 Likes

I’ve also got a feeling there’s something wrong with the ‘Rank’ part, which could relate to that Argument 1 missing/nil error. I’ll have a check through that.

2 Likes

Your theory was right, nothing printed.

1 Like

Update: Still having issues, looked through my code and I cannot figure out why. Help is appreciated.

1 Like

Updated this post with the new code, still not working.

cc @YummyYorkie123

1 Like

Is there Data in the Data store?

1 Like

Yes. There is data of around 5 people.

1 Like

Hmm try setting some different fake users using :SetAsync()

You can remove them later

OrderedDataStore:SetAsync("Jonny", 56)
OrderedDataStore:SetAsync("Mary", 68)
OrderedDataStore:SetAsync("John", 99)

tell me if it works

1 Like

It doesn’t make a difference. Per the tutorial I used, it should work even with one person.

1 Like

Sorry for being late
But im going to test your code to see if it works give me a moment

2 Likes