Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How do I use GetFriendsAsync()?

Asked by
Rawblocky 217 Moderation Voter
7 years ago

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?

0
I've come back to look back on some of my answers. Looking back, I've got to say, this was a good question. I am editing a help article and am using this as an example. M39a9am3R 3210 — 7y

1 answer

Log in to vote
4
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago

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

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.
0
Thank you so much! :D Rawblocky 217 — 7y
Ad

Answer this question