Quick Intro: Hey Devs! Some of you may remember me on the DevForum a couple years ago. Well, after a long absence (3 years,) I am back!
I was wondering how to create a GUI that randomly picks a player for my new game, similar to that of the one in Hide and Go Seek Extreme, where it displays a player’s avatar and username while flipping through each player in the server.
you can use for i, player in pairs(Players:GetChildren() do to loop through all players.
if you want to get just a image of the player’s avatar you can use :GetUserThumbnailAsync, or you can use viewportframes if you want to get all the player avatar or from a custom angle and camera.
local function pickRandomValue(arr: {}): any
return #arr > 0 and arr[math.random(#arr)] or nil
end
local function pickRandomPlayer(): Player?
local players = game.Players:GetPlayers()
return pickRandomValue(players)
end
local Players = game:GetService("Players")
local noIconFound = "rbxassetid://0" -- Put any image you want to show if you can't get the player's Icon
local function GetPlayerIcon(Player : Player)
local userId = player.UserId
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
return (isReady and content) or PLACEHOLDER_IMAGE
end
local function pickRandomPlayer()
local PlayerList = Players:GetPlayers()
local RandomNumber = math.random(1, PlayerList)
local selectedPlayer = PlayerList[RandomNumber]
print(`Selected a Player: ${selectedPlayer}`)
local plrIcon = GetPlayerIcon(selectedPlayer)
--Set the ImageLabel on the GUI to plrIcon
end
This should do what you’re hoping for. The only thing it isn’t doing is the flipping through the players. It just picks a random player and get the headshot of the picked player.
GetPlayerIcon() is taken right from Roblox’s Documentation