How can i load a random friend from a player's friend list?

Hello,
I am currently making a simple game where random npc’s are in-game player’s friends. you can interact with them and do stuff yourself around the map. but i really have no idea how to work with Players:GetFriendsAsync or HumanoidDescription, I have tried to code this and looked on other threads but i don’t quite understand how it works.

any help is appreciated, thank you in advance

2 Likes

There is a code sample at the bottom of GetFriendsAsync wiki page which stores the username of each friend in a table.

This is found at the bottom of the code sample (after line 27)

local usernames = {}
for item, pageNo in iterPageItems(friendPages) do
    table.insert(usernames, item.Username)
end

You can change this to store their Id instead like so

local friends = {}
for item, pageNo in iterPageItems(friendPages) do
    table.insert(friends, item.Id) -- change store Id instead of Username
end

You can then choose a random Id using math.random

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

And finally load up their character on a dummy using HumanoidDescription

local npc = game.Workspace.npc
local newHumanoidDescription = game.Players:GetHumanoidDescriptionFromUserId(randomFriend)
npc.Humanoid:ApplyDescription(newHumanoidDescription)
17 Likes

i thought i knew how to do this but i don’t lol
how can i incorporate this into my game?

I’d recommend you utilize HTTPService and send GET /users/{userId}/friends request. (https://api.roblox.com/users/{userId}/friends) You should then randomly select a child from the decoded friend return.

function print_random_friend()
plr = game.Players.LocalPlayer
friend_url = 'https://api.roblox.com/users/'..plr.UserId..'/friends'
local friends = game:GetService("HttpService"):GetAsync(friend_url)
math.randomseed(tick())
--Rest of the script
1 Like