GetFriendsAsync()
gathers "pages" of friends a user has. When you call :GetCurrentPage()
on the page, it will return a table of tables of 50 friends.
2 | { table, table, table, table, ... } ; |
3 | { table, table, table, table, ... } ; |
4 | { table, table, table, table } ; |
You should be able to iterate through the pages, then iterate through all of the tables within the page, to get all of the friends a user has. Hopefully, the code above helps you visualize it a little better.
The values every individual table will hold are Username, IsOnline, AvatarUri, Id, and AvatarFinal.
Whenever you call AdvanceToNextPageAsync()
it will move on to the next set of friends.
Initially, we want to call the GetFriendsAsync YieldFunction. Since we know no one may have any more than 200 friends, 50*4 = 200. We can set a numeric for loop for 1 through 4. We can then iterate through the table of the current page you are on.
At this point, if you wanted to gather all of the UserIds of the person's friends, you could gather them with friend["Id"]
.
For my example, I just simply looped through all of the tables to gather information.
I then used an if statement to determine if I was on page 4 or not. If I was, I would not attempt to continue on to the next page. However, since not everyone has 200 friends, I used a pcall. pcall()
or "protection call" will allow a function to run on its own without impacting the actual script. pcall()
will return two values, if the function ran successfully, and (if there was an error) what the error was.
As AdvanceToNextPageAsync()
would error out if there was no page to advance to, I believe the pcall was the necessary option. I then did a check if the first value pcall returned was the opposite of true for running successfully. If the first value was the opposite of true (false), then the if statement would execute its code. All that is in the if statement is break, which will break the script out of the numeric 1 to 4 for loop.
01 | local friendsList = game:GetService( "Players" ):GetFriendsAsync( 16826035 ) |
05 | for _,friend in pairs (friendsList:GetCurrentPage()) do |
06 | for key,value in pairs (friend) do |
07 | print (key .. ": \t" .. tostring (value)) |
13 | local pass,err = pcall ( function () |
14 | friendsList:AdvanceToNextPageAsync() |
Hopefully this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to leave them in the comments below.