Getting a players friends

Tried looking through here Players | Documentation - Roblox Creator Hub but made no sense to me

local PlayersFriends = Players:GetFriendsAsync(Player.UserId)

print(PlayersFriends)

for i, v in pairs(PlayersFriends) do
	print(i, v)
end
1 Like

Did you read/run the example on the page at all?

8 Likes

That function returns a FriendsPage. You can’t just directly attempt to iterate through without using GetCurrentPage().

You’ll need to do FriendsPage:GetCurrentPage() — then use .IsFinished to check if you need to use AdvanceToNextPage() to grab more or not.

1 Like

Expanding on @CodeSleepRepeat’s point, it would look something like this:

local players = game:GetService("Players")

local friendsId = 82347291
local PlayersFriends = {}

local success, page = pcall(function() return players:GetFriendsAsync(friendsId) end)
if success then
   repeat
   	local info = page:GetCurrentPage()
   	for i, friendInfo in pairs(info) do
   		table.insert(PlayersFriends, friendInfo)
   	end
   	if not page.IsFinished then 
   		page:AdvanceToNextPageAsync()
   	end
   until page.IsFinished
end


for i,v in pairs(PlayersFriends) do
   print("Username = ".. v.Username.." | UserId = ".. v.Id)
end
Username = TheTreeSeeker500 | UserId = 491808576 
Username = jwjr2008 | UserId = 121500779
Username = PHFCER21 | UserId = 145933876
Username = bananearon1 | UserId = 134852716
Username = yourmother2004 | UserId = 86285502
...

It is a bit of an unusual documentation. I remember struggling with this at first.

31 Likes

Hello, I am asking how to make so it only prints 3 players for example?

Don’t know if this is valid but:

for i = 1, 3 in pairs(PlayersFriends) do
    print("Username = ", v.Username, " | UserID = ", v.ID
end

or

for i, v in pairs(PlayersFriends) do
   if i ~= 3 then
      print("I don't wanna write it again but yeah")
   end
end

Also or the second one you might have to do if i ~= 4 instead of 3 and also you might have to do i += 1 each rep.

1 Like