How to get the friends of a player?

I was wondering, how can I get the friends of a player?

I don’t really get how to use it.

local Players = game:GetService("Players")
 
local USERNAME = "Shedletsky"
 
local function iterPageItems(pages)
	return coroutine.wrap(function()
		local pagenum = 1
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				coroutine.yield(item, pagenum)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
			pagenum = pagenum + 1
		end
	end)
end
 
-- First, get the user ID of the player
local userId = Players:GetUserIdFromNameAsync(USERNAME)
-- Then, get a FriendPages object for their friends
local friendPages = Players:GetFriendsAsync(userId)
-- Iterate over the items in the pages. For FriendPages, these
-- are tables of information about the friend, including Username.
-- Collect each username in a table
local usernames = {}
for item, pageNo in iterPageItems(friendPages) do
	table.insert(usernames, item.Username)
end
-- and finally, print!
print("Friends of " .. USERNAME .. ": " table.concat(usernames, ", "))

I know I saw it. The thing is I don’t get how to use it. Not an example.

Ok well.
For example we have player.
Player:GetFriendsAsync(ourplayerID)
It gets a table of names of friends from player.

1 Like