GetFriendsAsync() only returns 50 friends

Currently, Players:GetFriendsAsync() only returns 50 friends, even after iterating through all pages. This has been causing issues in my game and it was first reported by players 2 days ago (10/23).

The code used is the one from the documentation, which is supposed to return a table of ALL friends.

Here is the code:

local Players = game:GetService("Players")

local USERNAME = "Headstackk"

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
print(#usernames, usernames)

And the output shows a table with only 50 friends.

50  ▼  {
        [1] = "OMGIsPerry",
        [2] = "transquilleo",
        [3] = "Valkyrixk",
        [4] = "MengyamR2D",
        [5] = "oTheSilver",
        [6] = "maxgenadiy",
        [7] = "TToAnnihilate",
        [8] = "pulledupinnabeamer",
        [9] = "TheDomshoe",
        [10] = "PannRBLX",
        [11] = "TheKowloonPeak",
        [12] = "spylol911",
        [13] = "Auptnue",
        [14] = "marisawins",
        [15] = "Mr_EliteReturned",
        [16] = "SanserioP",
        [17] = "CrazyCodeX",
        [18] = "Strivian",
        [19] = "TheDiamondDrone",
        [20] = "ELEVEN_BILL",
        [21] = "nici_lp",
        [22] = "ToppatNoob",
        [23] = "Tatt_27",
        [24] = "Peeperushki",
        [25] = "AragenTrevino",
        [26] = "jjsam07",
        [27] = "Capra_K",
        [28] = "A_Renegade",
        [29] = "C9Z9",
        [30] = "Hyorc",
        [31] = "WindyEvolution",
        [32] = "Xurnz",
        [33] = "zec105",
        [34] = "ikepik3",
        [35] = "o_Corp",
        [36] = "AyPandaa",
        [37] = "Fr0stByt3X",
        [38] = "lakeinsilvano",
        [39] = "HKmedlc",
        [40] = "ImChompo",
        [41] = "asfarasyoucan",
        [42] = "funnyobesewalk",
        [43] = "iGottic",
        [44] = "Risyari",
        [45] = "GoSinister",
        [46] = "HowsLifeJosh",
        [47] = "xOATLEAFx",
        [48] = "superiorToYourMayo",
        [49] = "RK800ConnorMkI",
        [50] = "dekbimo"
     } 

Expected behavior

GetFriendsAsync() should return ALL friends of a user.

For some reason, coroutine.yield(item, pagenum) line stops continuing the loops after the first page is complete. Commenting out this while printing out all the items allows it to continue as normal.

local Players = game:GetService("Players")

local USERNAME = "Headstackk"

local function pagesToTable(pages)
	local items = {}
	while true do
		table.insert(items, pages:GetCurrentPage())
		if pages.IsFinished then
			break
		end
		pages:AdvanceToNextPageAsync()
	end
	return items
end

local function iterPageItems(pages)
	local contents = pagesToTable(pages)
	local pageNum = 1
	local lastPageNum = #contents
	
	return coroutine.wrap(function()
		while pageNum <= lastPageNum do
			for _, item in ipairs(contents[pageNum]) do
				coroutine.yield(item, pageNum)
			end
			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
print(#usernames, usernames)

I borrowed the example from the Pages documentation and it seems to work correctly this way. I think the culprit might have been the additional asynchronous operation with trying to advance to the next page.

Results should be, if I executed with my own username:

200  ▶ {...}
1 Like

Guess this is now a documentation error as the code example was taken from the official documentation, thanks!

1 Like

Hello!

This is related to a change we made that now has to make async calls per page to allow us to increase the friend limit over 200. As before it was 1 async call at the start for all friends and the paging would just load more from the cache.

I’ll update the example on monday to avoid the confusion in the future.

Thanks!

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.