GetNameFromUserIdAsync strange behavior

Hi there!

For context, I’m using :GetNameFromUserIdAsync() in unison with a global data store to retrieve players’ names based on their ID, with the key of the data being their ID.

I ran into an issue that required me to begin using pcall (as I should have done from the beginning) on the Async call. After switching to using pcall, my code has broken completely.

But I don’t think this is a simple bug with my code. I’ve ran over the code multiple times and added some prints for debugging. After doing so, I made some strange discoveries.

I’ve ensured the keys for my Datastore values are fine with prints, they are the proper user IDs. Using the command line and running :GetUserFromUserIdAsync() returns the username with no problems. HOWEVER, even when the pcall is successful, it’s returning nil. I did some deeper digging and realized the following:

When the pcall is NOT successful, I get the following error message:

false Players:GetUserIdFromNameAsync() failed: Unknown user -1

Okay, I can understand this error. It’s the reason I had to use pcall in the first place, as there are players in the datastore from studio test sessions with IDs <1.

HOWEVER, what does NOT make sense, and I believe what is causing the successful pcalls to return nil, is that it says I’m calling GetUserIdFromNameAsync - and I am not. I’ve checked, double checked and triple checked to ensure my code is indeed GetNameFromUserIdAsync and it is, as shown below:

local success, plrName = pcall(function() 
				
	Players:GetNameFromUserIdAsync(data.key)
				
end)

So why on earth is it returning nil even when successful, and why does it think I’m calling GetUserIdFromNameAsync (when I evidently am not)

Is this a studio bug? Have I done something wrong?

Give this a shot.

local success, plrName = pcall(function() 
   return Players:GetNameFromUserIdAsync(data.key)				
end)
3 Likes

Wow, I should’ve caught this so much sooner. Fixed!
Thank you so much!

On a side note, however, why was the error indicating I was calling GetUserIdFromName? What in the world is the cause of that?

1 Like

Justs a small thing I recommend doing:

local Players = game.Players

local success, data = pcall(Players.GetNameFromUserIdAsync, Players, self.key)

Much simpler in my opinion.

2 Likes

Hey, thanks for the tip! I always forget pcall can be used with this format. I never remember because spawn did not allow for this syntax (although I believe task.spawn() does allow for it now) and I have used spawn substantially more than pcall

1 Like

To add to this, negative user IDs are fake and refer to the studio test players(Player1, Player2, etc.) you should keep the pcall as @alphadoggy111 recommended since this will ensure no other errors will occur and add this line above it so you don’t need to make useless API requests:

if data.key < 0 then 
	print("This datastore entry belongs to Player"..math.abs(data.key).."(Bot)")
	--Handle it as you like(I suggest skipping them as many leaderboard scripts do)
end 
1 Like

Heya, thanks for the reply.

I know about the negative user ID issue, what’s strange to me is that the pcall error was indicating that I was calling GetUserIdFromNameAsync when I was calling the opposite. That was the main thing throwing me off, a bit of a red herring. I totally overlooked returning the result because I thought the engine was somehow replacing my GetName request with a GetUserId request

That’s either a bug or that function is being called internally inside the GetNameFromUserIdAsync function and is the function that throws the error.

1 Like

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