Scripting Help Roblox Studio

Anyone help me I want to know about friends service in roblox.So when I tried to do something nothing worked can anyone tell me how to print all player’s friends name.

script.Parent.MouseButton1Click:Connect(function()
local friends = game.Players:GetFriendsAsync(game.Players.LocalPlayer)
print(friends)
end)

1 Like

The :GetFriendsAsync() method asks for the user’s UserId, not the player directly.

This should work:

local friends = game.Players:GetFriendsAsync(game.Players.LocalPlayer.UserId)
2 Likes

Yes thank you but the players friends did not print. It got printed as Instance.

Hello. I’m a beginner scripter so I’m unable to provide adequate assistance in terms of actual scripting, however I think you should check out this documentation.

Towards the bottom of the page, you can find information on how to print friends.

Hope this helps!

1 Like

The function returns a FriendPages, object which contains information about the player’s friends. It can return 3 parameters: Id, UserName, IsOnline. You can’t get that information by directly calling the function.

When calling this function, you need to use GetCurrentPage, IsFinished, AdvanceToNextPageAsync() and a pcall.

GetCurrentPage returns the items on the current page.
IsFinished is a bool value that determines whether the current page is the last page available or not.
AdvanceToNextPageAsync() iterates to the next page, if possible.

Take a look at the API reference and experiment with the code provided on that page. If you need more help, feel free to ask :slight_smile:


An example code snippet:
Summary
local players = game:GetService("Players")
local player = game.Players.LocalPlayer

local Id = player.UserId
local Friends = {}

local success, page = pcall(function() 
	return players:GetFriendsAsync(Id)
end)

if success then
	repeat
		local PageInfo = page:GetCurrentPage()
		for _, friendUsername in pairs(PageInfo) do
			table.insert(Friends, friendUsername)
		end
		if not page.IsFinished then 
			page:AdvanceToNextPageAsync()
		end
	until page.IsFinished
end


for i,v in pairs(Friends) do
	print(v.Username)
end
3 Likes

Thank you. It worked for me.Thanks alot