Players:GetUserIdFromNameAsync() failed: Unknown user

Hello!

I made an global leaderboard script. The issue is that it says it’s an unknown user, and I don’t know what’s wrong with it. I tried switching GetNameFromUserIdAsync to GetUserIdfromNameAsync, but it said the same error. I searched the forums, but couldn’t get anything, nor they were related to my issue. Any help is appreciated!

local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetOrderedDataStore("WinsLeaderboard")

local function updateLeaderboard()
	local success, errormessage = pcall(function()
		local Data = WinsLeaderboard:GetSortedAsync(false, 5)
		local CanesPage = Data:GetCurrentPage()
		for Rank, data in ipairs(CanesPage) do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local Canes = data.value
			local isOnLeaderboard = false
			
			for i, v in pairs(game.Workspace.Leader.SurfaceGui.Holder:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end
			
			if Canes and isOnLeaderboard == false then
				local newFrame = game.ReplicatedStorage:WaitForChild("UserLeader"):Clone()
				newFrame.Player.Text = Name
				newFrame.Canes.Text = Canes
				newFrame.Rank.Text = "#"..Rank
				newFrame.Position = UDim2.new(0, 0, newFrame.Position.Y.Scale + (.08 * #game.Workspace.Leader.SurfaceGui.Holder:GetChildren()), 0)
				newFrame.Parent = game.Workspace.Leader.SurfaceGui.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, Player.leaderstats.Canes.Value)
	end
	
	for _, frame in pairs(game.Workspace.Leader.SurfaceGui.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	print("updated")
	
	wait(10)
end
4 Likes

This error usually occurs when the userid is not valid. If the userid is below 0 then it will error. I recommend wrapping it in a pcall. Here is an example of what I would do

local name = "Failed to load name"
local success, err = pcall(function()
    name = game.Players:GetNameFromUserIdAsync(tonumber(data.key)
end

This defaults to the string defined at the top and you might want to change it if you don’t like that

5 Likes

Works, sorta.

RobloxScreenShot20201222_133835614 (2)

2 Likes

The name ended up being the string

2 Likes

For this you could just avoid going further if the pcall fails but that is up to you to figure out if you want that. Nonetheless here is the code you could use for skipping the user

if not success then continue end
2 Likes

Problem is what the data key really is. It is possible that it is not using only numbers but a concatenated string including the numbers.

2 Likes

So, how do I fix that???

3 Likes

If I have to point the simple thing out, try printing the key or alternatively find the functionalities that is responsible for updating that data store, describe how each key is written to the data.

2 Likes

Did that, it printed out my UserId, and the error still persists.

2 Likes

I found a topic of similar issue. Any solution that applies?

Also where are you testing this?

2 Likes

I tested it in both Studio and the client, they both have the same error.

2 Likes

Tested it in studio, it got an error:

ServerScriptService.Script:9: invalid argument #1 to 'pairs' (table expected, got Instance)
2 Likes

I put Data in the 1st argument.

2 Likes

Did you figure it out or do you still need help?

2 Likes

It fixed itself somehow, but the error is still going in output, and it shows a person who played my game once on the leaderboards, even though i’m the richest one. The rank also somehow shows the person as #2, when it’s not.

Here’s the picture:
RobloxScreenShot20201222_150300207 (2)

2 Likes

To answer your question, yeah, i still need help.

1 Like

And nothing is being added to the leaderboard except for the player.

1 Like

Might be late but can I asked:
how did you get passed the errorMessage and written your playername on it?

I have a similar problem and can’t get around it.
and I get the same Error inside the Output.
“Read Topic Name”

1 Like