I fetch a user’s friends along with relevant data to be displayed in an in-game social tab, but lately this method has started consistently failing which unfortunately results in my game losing important social functionality.
Relevant methods:
- Players:GetFriendsAsync()
- FriendPages:GetCurrentPage()
- FriendPages:AdvanceToNextPageAsync()
Here’s a relevant snippet from my code:
type OnlineFriends = {
[number]: {
UserId: number; -- The friend's UserId
Username: string; -- The friend's username
DisplayName: string; -- The display name of the friend
PlaceId: number?;
}
}
local onlineFriends: OnlineFriends = {}
local success, err = pcall(function()
local friendPages = game.Players:GetFriendsAsync(player.UserId)
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
for item, _pageNo in iterPageItems(friendPages) do
if not item.IsOnline then continue end
local data = {
UserId = item.Id;
Username = item.Username;
DisplayName = item.DisplayName;
PlaceId = 0; -- fetched with TeleportService
}
table.insert(onlineFriends, data)
end
end)
if (not success) then
warn(("[Friends] Couldn't fetch friends: %s"):format(err))
return
end