Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to take a headshot of a random player and put it on a ImageLabel?

I am working on a story game and i need a random players headshot and put it on an ImageLabel.

--// Variables
local players = game.Players:GetChildren() 
local randomplayerid = players[math.random(1,#players)].PlayerId
local thumbSize = Enum.ThumbnailSize.Size420x420
local thumbType = Enum.ThumbnailType.HeadShot
local imageLabel = script.Parent

--// making the headshot of the player
local content, isReady = players:GetUserThumbnailAsync(randomplayerid, thumbType, thumbSize)
--// adding the headshot image on the ImageLabel
imageLabel.Image = content

1 answer

There are a few instances I would change in your code.

You used players to get the children of the player's service rather then service itself, so it would error when use it in line 9. Furthermore, PlayerId should be UserId. I would personally declare your variables a bit more organised, so it's a bit more clear.

-- Variables
local players = game.Players:GetPlayers() 
local randomPlayer = Players[math.random(1, #Players)]
local userId = randomPlayer.UserId

local thumbSize = Enum.ThumbnailSize.Size420x420
local thumbType = Enum.ThumbnailType.HeadShot
--// making the headshot of the player
local content, isReady = game:GetService("Players"):GetUserThumbnailAsync(userId, thumbType, thumbSize)

local imageLabel = script.Parent
--// adding the headshot image on the ImageLabel
imageLabel.Image = content

This discussion is preserved as it was. New answers, comments, and votes are closed.