Grab local player 1 random friend from there friend list, and display it on a Image Button/Label

Hello! I am here to try and find help on how to get a players random friend from there friend list, and display there headshot onto a image button.

I have tried looking at more DevForums, but I can seem to figure it out, and get the user ID from the random players username to display there head avatar.

Here is the code I user to get the players headshot onto a image button, and it works. Now I just need to figure out how to change it to being the local player, to it being the local players random friend, and I am hoping you guys can help me out!

Here is a picture of what it will look like. (FYI: I only need 1 random friend atm, I will just duplicate the script into each image button once we have figured it out.)
image

This is the code to add the local players head onto a image label, and what it looks like (This works.)

local frame = script.Parent

local player = game.Players.LocalPlayer

local userId = player.UserId
local thumType = Enum.ThumbnailType.AvatarBust
local thumSize = Enum.ThumbnailSize.Size420x420
local content = game.Players:GetUserThumbnailAsync(userId, thumType, thumSize)

frame.Image = content

You can use Players:GetFriendsAsync(UserId) to get a players friends. I recommend reading the code sample as it uses paging

The ratelimit on this endpoint is 20/minute

Hey, so I took a look and the code sample shown made you put in a username to get the ID, and isn’t really what I wanted. I am simply just looking to get 1 random friend, and get the user ID from that 1 random friend. I found this on a dev forum, but I am not sure where to get the “friend” variable from.

local randomFriend = friends[math.random(#friends)]

Maybe I could get a outline of what to do?

as @Haydz6 said

is your solution. You put player(which friends you want to get) user id and you get a table of ALL his friends, then you just do math.random(1,#friends_table_name) and then take userID by this id and convert it to player name

1 Like

Okay, I took another look at it and read what you said. I think I understand, I replaced the “USERNAME” variable with my username and it printed all my friends, now I just need to figure out instead of having the local username being equal to a set username, I want it set to the local player. Thanks!

Alright!
I figured it out its work completely, thanks a lot!

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