Changing image labels to pfps (without needing the player to be in game)

So basically, I am making a sandbox game where you can place anything from the given database down inside the game and I’ve tried to make a billboard that displays my avatar profile picture. Thing is, I’ve never actually messed with profile picture images when it comes to coding it in, nor do I know how to put down the proper URL for it. If anyone could help me with knowing how to put in avatar picture URLS (along with the id if needed) it would be appreciated.

1 Like

Hello. You’ll have to use Player:GetUserThumbnailAsync(userID, ThumbnailType, ThumbnailSize) for the ImageLabel. This automatically gets the avatar image and displays it as an image. You can choose the size and the type - that is, if you want it to display the head, head and upper chest or entire avatar.

Read the API reference here!

5 Likes

The issue here is that I have tried this already but the output comes up with an error, more specifically it stated, “attempt to index nil with ‘UserId’”
heres a screenshot:


again, its a surface gui. You were probably thinking about implementing an image via a regular GUI (i could be wrong, I’m not that great at lua anyway). I need a way to be able to grab User ids from someone who doesn’t necessarily need to be in game (which is why i stated “my” profile picture), not from someone who needs to be in game at all times just so the system can pick their id up.

1 Like

It seems your script errors because it is a regular script, not a local script. Server scripts will return nil when indexing game.Players.LocalPlayer.

If I understood it, you need to display the avatar of, for example, the user who created something that belongs to that database, right? If it’s the case, you’ll need to store the ID and associate it with the related objects.

I need a way to be able to grab User ids

The ID is just a number. For example, if you put 29167537 instead of player.UserID, it will always display your avatar. What criteria are you going to consider to choose an user? This detail might help to achieve your objective.

1 Like

You fixed the problem on whether or not I had the correct script or not. But now for some reason the script is refusing to change the image at all, even though it is coming up with no errors whatsoever:


edit: and yes it is the correct user id for my profile picture

1 Like

It probably does nothing because you are using a LocalScript. Yes, this is what I told you before but:

  • LocalScripts are to be used by the client. They must only be parented to GuiObjects or StarterPlayerScripts/StarterCharacterScripts.
  • (Normal) Scripts should be used for stuff held by the server. They will run if they are parented to the Workspace or ServerScriptService.

As you can see, in this case you will need to use a regular script. Note that these can’t access a player by using game.Players.LocalPlayers.

1 Like

Ok that sums up the issue pretty well. Sorry for the slight incompetence in the situation xD

3 Likes

If you want to get the UserId of a Player who isn’t in-game, you’d need to use GetUserIdFromNameAsync.

Here’s a quick simple code I put together (Local Script):

PlayerService = game:GetService('Players')

local ImageLabel = script.Parent

function setPlayerIcon(PlayerName)
	local Success, Err = pcall(function()
		local PlayerId = PlayerService:GetUserIdFromNameAsync(PlayerName)
		local ThumbType = Enum.ThumbnailType.AvatarBust
		local ThumbSize = Enum.ThumbnailSize.Size420x420
		local Content, IsReady = PlayerService:GetUserThumbnailAsync(PlayerId, ThumbType, ThumbSize)
		
		ImageLabel.Image = Content
	end)
end

setPlayerIcon('ScriptedMoose') -- Replace 'ScriptedMoose' with any players name

For more info on GetUserThumbnailAsync: Players | Documentation - Roblox Creator Hub

2 Likes