How to use GetFriendsAsync()?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

im trying to make a function that returns a players friends ids

  1. What is the issue? Include screenshots / videos if possible!

For some reason it returns an empty table

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

yes but didnt find anything

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

the function:

local module = {}

function module:GetPlayerFriendsIds(userid)
	local PlayersFriends = {}

	local success, page = pcall(function() return game:GetService("Players"):GetFriendsAsync(userid) end)
	if success then
		repeat
			local info = page:GetCurrentPage()
			table.insert(PlayersFriends, info.Id)
			if not page.IsFinished then 
				page:AdvanceToNextPageAsync()
			end
		until page.IsFinished
	end
	return PlayersFriends
end

return module
1 Like
local module = {}

function module:GetPlayerFriendsIds(userid)
	local PlayersFriends = {}
	local success, output = pcall(function() 
		-- wrap the entire call inside of a pcall, not just the initial part
		local list = game:GetService("Players"):GetFriendsAsync(userid) 
		-- the API call returns a table
		while true do -- loop through all the pages
			for _, data in list:GetCurrentPage() do
				table.insert(PlayersFriends, data.Id) -- add the friend's userid to the table
			end
				
			if list.IsFinished then
				-- stop the loop since this is the last page
				break
			else 
				-- go to the next page
				list:AdvanceToNextPageAsync()
			end
		end
	end)
	
	if not success then
        -- lets you know if a problem occured somewhere
		warn(output)
	end
	
	return PlayersFriends
end

return module

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