I wanna get access to a list of a player's friends and the amount they have. http://wiki.roblox.com/index.php?title=API:Class/Players/GetFriendsAsync Can someone demonstrate how to use it?
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.
Book = { {table, table, table, table, ...}; {table, table, table, table, ...}; {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.
local friendsList = game:GetService("Players"):GetFriendsAsync(16826035) --Had to use a friend's UserId since he had more friends than I did. These are all of the pages of the player's friends. for page=1,4 do --4*50 = 200, you can not have more than 200 friends. for _,friend in pairs(friendsList:GetCurrentPage()) do --This will iterate through the page, friend is a table for one of fifty friends on the page. for key,value in pairs(friend) do --This will iterate through the friend table's values. This will present the Id, AvatarUri, and Usernames of the friends. print(key .. ": \t" .. tostring(value)) --Just printing values. end end if page ~= 4 then --If this isn't page 4, go into the if statement. local pass,err = pcall(function() --Set up a pcall function friendsList:AdvanceToNextPageAsync() --If we can not AdvanceToNextPage, then the pcall will error resulting in pass being false. end) if not pass then --If pass is true, then the not will negate that making the if statement false and not perform the logic. If pass is false, then the not statement will negate that and run the if statement as true. If statements can only run on true statements. break --Break out of the 1 through 4 loop. end end end