How can I get a players name via their userid if they arent in the game?

So as said in the title I want to do this, I tried doing players:GetPlayerByUserId() but apparently it only works when they are in the server, so how can I do it?

1 Like

Are you trying to do this script in the client? Also, why would you need to do this in the client (if applicable). The script can’t run on the client if they are not in the game!

I believe :GetPlayerByUserId() only works if the player is in the game.


Edit: I think I understand what you are trying to say so give me a minute

1 Like

Well yes Im trying to do this on the client but Im not trying to get my player I’m trying to get the name of another player

1 Like

You can use the:GetNameFromUserIdAsync() --fill the brackets with the players username function on the players service to the the players name. You can use it as a variable and it will return a string!

It should be able to run on the client


Hope this helps!

2 Likes

You would need to use the Roblox API, specifically the https://users.roblox.com/v1/users/{userId} endpoint which requires as parameter the user ID.

local HttpService = game:GetService("HttpService")
local endpoint = "https://users.roproxy.com/v1/users/" -- Use RoProxy as Roblox doesn't allow HTTP requests to its own domain.

function GetUsernameById(id)
	return HttpService:JSONDecode(HttpService:GetAsync(endpoint .. id, false)).name -- Return username of the player.
end

The script above should work, make sure to have HTTP requests enabled.

1 Like

This code would also work, I’d recommend to use this over my example.

1 Like

(opinion) Accessing an HTTP service is completely unnecessary. You don’t need to go beyond the Lua threshold to find a players UserId via script.

I wasn’t aware of the existence of this method under the Players service.

1 Like

This always ends up happening

sell:FindFirstChild('Display'):FindFirstChild('Owner').Text = '[OG OWNER: '..players:GetNameFromUserIdAsync(tonumber(Selected:FindFirstChild('OgId').Value)).Name..']'

Players.FroDev1002.PlayerGui.Market.MarketControl:101: attempt to concatenate nil with string
thats the error I get, the textlabel that Im trying to set its text to is there btw

You’re not supposed to add .Name to the result.

Code:

sell.Display.Owner.Text = '[OG OWNER: '..players:GetNameFromUserIdAsync(tonumber(Selected.OgId.Value))..']'
1 Like

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