Is there anything I need to do to make `GetFriendsAsync` work?

I am using roblox-ts.

I can’t get any FRIEND information at all when I run the following code.
I have tried by running it on both ServerSide and ClientSide.
Id will be my UserId.

private createNpc(): void {
		print("createNpc");
		const friendPages = Players.GetFriendsAsync(5154434305);
		while (!friendPages.IsFinished) {
			print("friendPages.IsFinished");
			const friends = friendPages.GetCurrentPage();
			for (const friend of friends) {
				const friendUserId = friend.Id;
				const friendName = friend.Username;
				print("friendUserId: " + friendUserId + " friendName: " + friendName);
			}
			friendPages.AdvanceToNextPageAsync();
		}
}

There is no particular Error, etc., and it returns empty.

The lua code output by roblox-ts is shown here.

function PlayersManager:createNpc()
		print("createNpc")
		local friendPages = Players:GetFriendsAsync(5154434305)
		while not friendPages.IsFinished do
			print("friendPages.IsFinished")
			local friends = friendPages:GetCurrentPage()
			for _, friend in friends do
				local friendUserId = friend.Id
				local friendName = friend.Username
				print("friendUserId: " .. tostring(friendUserId) .. " friendName: " .. friendName)
			end
			friendPages:AdvanceToNextPageAsync()
		end
end

while i am not particularly sure what your issue is, there is an official ROBLOX code sample which does this aswell. perhaps try this snippet instead?
code found from: https://create.roblox.com/docs/reference/engine/classes/Players#GetFriendsAsync

local Players = game:GetService("Players")

local USERNAME = "Cozecant"

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("Friends of " .. USERNAME .. ": " .. table.concat(usernames, ", "))```
1 Like

Thanks! I modified the loop condition to true and used friendPages.IsFinished to break the loop, and now it works correctly.

This is the code for roblox-ts.

private createNpc(): void {
		print("createNpc");
		const friendPages = Players.GetFriendsAsync(5154434305);
		while (true) {
			print("friendPages.IsFinished");
			const friends = friendPages.GetCurrentPage();
			for (const friend of friends) {
				const friendUserId = friend.Id;
				const friendName = friend.Username;
				print("friendUserId: " + friendUserId + " friendName: " + friendName);
			}
			if (friendPages.IsFinished) {
				break;
			}
			friendPages.AdvanceToNextPageAsync();
		}
}