Need help with using a UserID in script

How do I use a persons UserID from their profile in a script and for example have the script find their account name?

Use this link to learn what UserId is, and where It can be found

The player instance has a UserId property if you want to grab a player’s id in game. You can then use Players:GetNameFromUserIdAsync to grab anyone’s username just by specifying the ID.

2 Likes

How would I use this in a script? Could you give an example

Here’s an example.

(Localscript in startergui)

local player = game.Players.LocalPlayer
print(player.UserId)

no no, the Players:GetNameFromUserIdAsync thing

where do i put the UserID to get the name

local username = game:GetService(“Players”):GetNameFromUserIdAsync(012345678)

I recommend wrapping it in a pcall and caching names.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local UserId = Player.UserId
print(Players:GetNameFromUserIdAsync(UserId))

The way I’m trying to use it wont work,


local Players = game:GetService("Players")
local Input = script.Parent.Input
local Output = script.Parent.Output
Input.Text.Changed:Connect(function()
	local Convert = Players:GetNameFromUserIdAsync(Input.Text)
	Output.Text = Convert
end)

If this does not work let me know.


Doesn’t work, gets an error at line 5

local Players = game:GetService("Players")
local Input = script.Parent.Input
local Output = script.Parent.Output
Input.Changed:Connect(function()
	local Convert = Players:GetNameFromUserIdAsync(Input.Text)
	Output.Text = Convert
end)

try this!

Works!


do you by any chance know how to do this with AvatarIcons?

like displaying there avatar? on a imagelabel thing

Mhm, Yeah. Do you know how to do that? I know how to do it regularly with a local player but i dont know how to do it like this

well do you have any code that displays a players icon or whatever so I can edit it to work with this

Yeah

local PlayerImage = script.Parent:WaitForChild(“ImageLabel”)

PlayerImage.Image = game.Players:GetUserThumbnailAsync(game.Players.LocalPlayer.UserId,Enum.ThumbnailType.AvatarThumbnail,Enum.ThumbnailSize.Size420x420)

local Players = game:GetService("Players")
local Input = script.Parent.Input
local Output = script.Parent.Output
local ImageOutput = script.Parent.Headshot
Input.Changed:Connect(function()
	local Convert = Players:GetNameFromUserIdAsync(Input.Text)
	Output.Text = Convert
	ImageOutput.Image = Players:GetUserThumbnailAsync(tonumber(Convert), Enum.ThumbnailType.AvatarThumbnail, Enum.ThumbnailSize.Size420x420)
end)

add a imagelabel thing called Headshot next to the output textlabel

The Headshot doesn’t work. I got this error