Can't get Player Username from data store key

I’ve attempted to retrieve a Players username from a data store but can’t seem to be successful with it.

Here’s the code:

for position, v in ipairs(topPage) do
		local UserId = v.key
		print(v.key)
		print(Players:GetPlayerByUserId(v.key))
		local Username = tostring(Players:GetPlayerByUserId(UserId))

Output:
image

Hi there!
could you try

Players:GetPlayerByUserId(tonumber(v.key))

1 Like

Nope, still the same issue.
image

Could you elaborate more for which line it tells the error

Item.Frame.Username.Text = Username

That means your Username is nil try printing it before doing

and see if it says nil

That’s not alot of code to understand the root problem

1 Like

Okay sorry, Here’s the full code.

local function GetTopPlayers()
	local data
	local succes, err = pcall(function()
		data = MainData:GetSortedAsync(Ascending, MaxItems, MinValue, MaxValue)
	end)
	
	if err then
		warn("An error Occured, Couldn't get Top Players!")
		print(err)
		data = MainData:GetSortedAsync(Ascending, MaxItems, MinValue, MaxValue)
	end
	
	local topPage = data:GetCurrentPage()
	
	for position, v in ipairs(topPage) do
		local UserId = tonumber(v.key)
		local Username = tostring(Players:GetPlayerByUserId(UserId))
		
		local Item = Template:Clone()
		Item.Name = "Place_"..tostring(position)
		Item.Visible = true
		Item.Parent = ScrollinFrame
		Item.Frame.Place.Text = tostring(position)
		Item.Frame.Value.Text = tostring(v.value)
		Item.Frame.Username.Text = Username
	end
end

while wait(UpdateTime * 60) do
	for i, Player in pairs(Players:GetPlayers()) do
		local leaderstats = Player:FindFirstChild("leaderstats")
		
		print(Players:GetPlayerByUserId(Player.UserId))
		
		if not leaderstats then
			warn("Could not find leaderstats!")
			break
		end
		
		local statsValue = leaderstats:FindFirstChild(StatsName)
		
		if not statsValue then
			warn("Could not find "..StatsName)
			break
		end
		
		local succes, err = pcall(function()
			MainData:UpdateAsync(tostring(Player.UserId), function()
				return tonumber(statsValue.Value)
			end)
		end)
		
		if err then
			warn("Could not Save data!")
			print(err)
			MainData:UpdateAsync(tostring(Player.UserId), function()
				return tonumber(statsValue.Value)
			end)
		end	
	end
	
	for i, Frame in pairs(ScrollinFrame:GetChildren()) do
		if Frame:IsA("ImageLabel") then
			Frame:Destroy()
		end
	end
	
	GetTopPlayers()
end

Forgot to mention that the leaderboard works on new keys, I’m trying to retrieve keys from an already existing data store.

The topPage might return nil
check the module or the code whatever you typed there you should always print values to check if they return what you need and not nil

I tried printing their usenames but they all returned nill, weirdly when i tried to manually get their Usernames by the userid i have printed it still has returned to nill.

You should try using other method then

Alright then, the only option I have to completely reset the data store and manually set their data in there.

Thanks for taking your time to reply to this topic have a wonderful day!

Have a good day too :smiling_face: and good luck

1 Like

It’s printing nil because the code you used was looking for the player instance. You should use game.Players:GetNameFromUserIdAsync

local Username = game.Players:GetNameFromUserIdAsync(UserId)

2 Likes

Exactly. I feel really bad for the OP because they thought they had to do a Data Reset

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