Players:GetUserIdFromNameAsync() failed: Unknown user

So I have the following function: Players:GetUserIdFromNameAsync() in my own function and it has no errors and works fine. The only problem is when I get this error: Players:GetUserIdFromNameAsync() failed: Unknown user, My entire leaderboard script breaks down completely and I don’t think there is a way to fix this other than adding a failsafe for the function. Can i use a pcall to check if the function was successful and break out of the function if not?

Obviously You Would Have A Variable Called Player And Not Player.Name
Just Do

Player.UserId

For Players Id

Not really. I failed to mention that this is returning results from an ordered datastore and only returns the players Id and not a player object.

In that case, you should be saving the player’s name in another data store.

The problem may be because the account with in the name does not exist, if you can use a pcall to check if the function works, but also make sure that the account also exists.

You can get more information here GetUserIdFromNameAsync

Im using something very similar to the link sent. Here is my code:

function getUsernameFromUserId(userId)
	-- First, check if the cache contains the name
	if cache[userId] then return cache[userId] end
	
	-- Second, check if the user is already connected to the server
	local player = Players:GetPlayerByUserId(userId)
	if player then
		
		cache[userId] = player.Name
		return player.Name
	end 
	
	-- If all else fails, send a request
	local name
	
	pcall(function ()
		name = Players:GetNameFromUserIdAsync(userId)
	
	end)
	cache[userId] = name
	
	return name
end 

local username = getUsernameFromUserId(tonumber(data.key)) -- This is how the function is used. This is used in the context of the code that isn't in this example.

It works fine except for a single account that gives this error: invalid argument #3 (string expected, got nil) referencing this line:

playerLeaderBoard.Player.Text = username

I see that in the error it says (expecting string, got nil) and also notice that you used in the local variable of “username” that you used the tonumber () function. This function takes an argument and returns it as a number and if the string does not appear as a number, the tonumber () function will appear as nil according to DevHub, Maybe you should try tostring (), with this you can convert numbers to strings.

You can get mor information about strings here Strings

Thats a problem. Does the Players:GetPlayerByUserId(userId) Method take strings for userids?

Oh I see, my mistake, but do you want to get username or userid?

@axelcerritos100 I want to get the username and put it on the leaderboard.