How to get DisplayName from UserId?

I am trying to make it so I can get the player’s displayname from their UserId. I have found out that I can use Players:getUserInfosFromUserIdsAsync() to get the DisplayName but when I try to do that it comes up with an error from the pcall.

 GetUserInfosByUserIdsAsync is not a valid member of Players "Players"  -  Client

I can’t figure out how I am suppose to do this. Can’t you use this function anymore?

Here is my code:

local userInfo
local success, errormessage = pcall(function()
	userInfo = game.Players:GetUserInfosByUserIdsAsync({userID})
end

if success then
	print(userInfo.Username)
	print(userInfo.DisplayName)
	playerInfoFrame:WaitForChild("DisplayName").Text = userInfo.DisplayName
	playerInfoFrame:WaitForChild("Username").Text = userInfo.Username	
else
	warn(errormessage)
end

If you can’t use GetUserInfosByUserIdsAsync() anymore, is there an alternative?

Any help is appreciated.

1 Like

The problem here is that you’ve said game.Players, when looking at the devhub of this function it is under the service “UserService” so try

local UserService = game:GetService("UserService")

UserService:GetUserInfosByUserIdsAsync({userID})

Edit: Just realised there is a couple more errors

You need to define newPlayerPage and the userId

local player = game.Players.LocalPlayer
local playerInfoFrame = player.PlayerGui.newPlayerPage:WaitForChild("playerInfo")
local UserService = game:GetService("UserService")

local success, result = pcall(function()
	return UserService:GetUserInfosByUserIdsAsync({80000})
end)

if success then
	for _, userInfo in ipairs(result) do
		print("Id:", userInfo.Id)
		print("Username:", userInfo.Username)
		print("DisplayName:", userInfo.DisplayName)
	    playerInfoFrame:WaitForChild("DisplayName").Text = userInfo.DisplayName
	    playerInfoFrame:WaitForChild("Username").Text = userInfo.Username	
	end
else
	warn(result)
end
4 Likes
local Players = game:GetService("Players")
local Player = Players:GetPlayerByUserId(UserId)
local DisplayName = Player and Player.DisplayName or ""

put the UserId where UserId is obviously

this shouldn’t work if the player isn’t inside the game though

I have experienced and fixed this issue a while back ago when I was making a resource.
Use this:

local ID = --UserID
local UserService = game:GetService("UserService")
local success, Info
success, Info = pcall(function()
	return UserService:GetUserInfosByUserIdsAsync({ID})
end)
local DisplayName = Info[1].DisplayName
--aditional
print(CreatorDisplayName) --DisplayName, if any

This should work for players that are not in the game. If not, you will need to use a proxy. I haven’t tested it outside.

Then just check if the player is descendant of Players. If so then the player is inside the game.

oh sorry my code works fine, if there are no player in the game with that id then DisplayName is “”

just saying you can’t get a player from outside the game

Oh oops I read that wrong sorry about that!

Ah, okay. It’s just weird because on the developer website it said Players in the code and thought it was in game.Players. It said Function of UserService, but I didn’t see that.
Thanks.

1 Like