DataStore Key is not the same

I’m trying to do a global leaderboard and for that I need ordereddatastorepages. When I set the key through :SetAsync(key, val) its the user id, but when I extract it using for i, v in pairs (data) do game.Players:GetPlayerByUserId(v.key).Name or GetUserIdFromNameAsync() it shows me a different key.

local l ="IntValue", plr)
l.Name = "l"
local Key = plr.UserId
local ds = game:GetService("DataStoreService"):GetOrderedDataStore("GlobalLB6")
local GetSaved = ds:GetAsync(Key)
if GetSaved then
	l.Value = GetSaved
	l.Value = 0
	local ds = game:GetService("DataStoreService"):GetOrderedDataStore("GlobalLB6")
	ds:SetAsync(plr.UserId, l.Value)
local ownitems = {}
local data = ds:GetAsync(plr.UserId)
if data then	
	ownitems = data

for i = 1, 20 do
	local sam = script.Sample:Clone()
	sam.Name = i
	sam.Parent = script.Parent.Frame.ScrollingFrame
	sam.Top.Text = tostring(i)
	sam.uName.Text = ""
	sam.Buys.Text = "..."
	sam.LayoutOrder = i
	script.Parent.Frame.ScrollingFrame.CanvasSize =, 0, 0, 50 * i)

function updateGui()
	for i,v in pairs (game.Players:GetPlayers()) do
		local Data = v:WaitForChild("l").Value
		local ds = game:GetService("DataStoreService"):GetOrderedDataStore("GlobalLB6")
		ds:SetAsync(v.UserId, Data)
	local ds = game:GetService("DataStoreService"):GetOrderedDataStore("GlobalLB6")
	local pages = ds:GetSortedAsync(false, 20)
	local data = pages:GetCurrentPage()
	for k, v in pairs (data) do
		if tonumber(v.key) >= 1 then
			local frame = script.Parent.Frame.ScrollingFrame:FindFirstChild(tostring(k))
			if frame then
				frame.uName.Text = game.Players:GetUserIdFromNameAsync()(v.key).Name
				frame.Buys.Text = ds:GetAsync(v.UserId)

	local ds = game:GetService("DataStoreService"):GetOrderedDataStore("GlobalLB6")
	ds:SetAsync(plr.UserId, plr.l.Value)

while true do

GetUserIdFromNameAsync accepts a string, that being the player’s username, not the id. The correct method would be GetNameFromUserIdAsync, which would accept the user id, and the correct way to write it would be game.Players:GetNameFromUserIdAsync(v.key), no need to add the .Name on the end.

If this doesn’t fix your problem, could you be more specific about when it shows you a different key, since that seems a little vague?

The problem is that when I save the key with :SetAsync(key…) I set it to the users ID. The thing is when I get the userid (key) from the pages to do the :getnamefromuserid… It is a different one from the one I saved in the : SetAsync

And therefore I can’t get the name since the user ID is incorrect

As it says here, you have to pass through the username (a string), but you pass through no arguments? Also why are you then calling it again, as if it worked then it would be calling a number. Similar thing with the .Name as you don’t need to index the return value.

Looking at this, you use the UserId as the key, but then you’re using GetUserIdFromNameAsync? This makes no sense, because you already have the UserId!

This also would be an issue, as there is no defined “UserId” in the table, to get the value you should instead use v.value

To fix your issues, you should get the Name from the UserId because you use the UserId as the key, and also use the values passed through instead of using GetAsync.

local success,name = pcall(game:GetService"Players".GetNameFromUserIdAsync,game:GetService"Players",v.key)
frame.uName.Text = (not success and "unknown") or name
frame.Buys.Text = v.value

Ok, firstly, the (v.key) outside the function is a mistake while copying it here. It is right in my script. Secondly, the key that I define at first is the key of a random player that joins the game, unlike the one that I’m trying to get from the datastore, Wich is the key from the players with more points. Therefore, the key of the top valorated players is not the key of a random guy who joins the game.

1 Like

Why not update the post? The .Name still isn’t needed, or the extra call.

What do you mean???
The value passed through is the same one that will be retrieved with GetAsync, you’re just using GetAsync an egregious amount of times. Due to the leaderboard refreshing every 15 seconds you quite possibly could encounter throttling.

Every time the player joins, you get their data twice. I don’t see any purpose because you’re getting it from the same datastore with the same key, so why not just use the same data instead of getting it again?