Roblox has officially released two highly anticipated methods that change how developers integrate social experiences into their games: Roblox GetFriendsInServerAsync and GetFriendsInUniverseAsync، These new APIs allow you to retrieve lists of a user’s friends either within the current server or anywhere across your entire game experience، By streamlining these lookups, Roblox aims to help creators build complex social features—such as in-game friends lists, teleport-to-friend buttons, and player markers—without the need for cumbersome manual filtering logic.
Roblox GetFriendsInServerAsync and: key details
Understanding the New API Methods
Previously, developers had to rely on a combination of existing methods like Players:GetPlayers and Player:IsFriendsWithAsync or Player:GetFriendsOnlineAsync to identify friends، This process was often inefficient, requiring multiple checks or complex server-side filtering to exclude players who weren’t relevant to the current query، The new APIs drastically reduce the overhead required to manage these lists.
You can find both methods under the Player class in the Engine API Reference، By utilizing these methods, you essentially offload the lookup logic to the Roblox engine, ensuring your scripts remain clean and performant.
How to Use GetFriendsInServerAsync
The Player:GetFriendsInServerAsync method is designed specifically to return an array of Player objects corresponding to the local player’s friends who are currently present in the same server، Because the method returns actual Player objects, you gain immediate access to useful properties like DisplayName, UserId, and Character، This makes it trivial to create UI elements that display names or to render world markers above friends’ heads.
It is important to note that the player you call this method on is not included in the returned array، If you are replacing older code that manually filtered Players:GetPlayers, you will find this significantly faster to execute، Here is a basic implementation example:
local Players = game:GetService("Players")
local localPlayer = Players،LocalPlayer
local ok, friendsInServer = pcall(function()
return localPlayer:GetFriendsInServerAsync()
end)
if ok then
for _, friendPlayer in friendsInServer do
print(friendPlayer،DisplayName, friendPlayer،UserId)
end
end
Implementing GetFriendsInUniverseAsync
While the server-specific API is great for local interactions, Player:GetFriendsInUniverseAsync is the preferred choice for cross-place social features، This method returns an array of tables for friends who are online anywhere within your specific game experience، This includes friends who might be in the same server as the player, as well as those currently playing in a different place (or instance) of your universe.
There is a critical restriction to remember: this method must be called only on Players،LocalPlayer، Attempting to call this on other players will result in an error، The returned table contains the friend’s User object, the PlaceId of their current location, and their server GameId، The PlaceId is currently returned as a string، This method effectively replaces the practice of using Player:GetFriendsOnlineAsync and filtering the results manually based on the universe ID.
Error Handling and Privacy Constraints
Because these APIs rely on network calls, they are susceptible to the same potential failures as any other asynchronous network operation، You must always wrap these calls in a pcall function، If a call fails, your game logic should be resilient enough to continue functioning—either by hiding the social UI elements or by reusing the last successfully retrieved result.
Privacy settings play a significant role in what these APIs return، If a friend has adjusted their privacy settings to prevent others from seeing their “Online status” or “Show current game,” they will not appear in the results for either of these APIs، Additionally, creators should be aware of regional restrictions، Specifically, for users in Australia who are under 16 years of age and have set their developer friends list access to “No,” these endpoints will return an empty array، Always treat an empty array as a normal, valid result, simply indicating that no friends were found in that specific context.
Source: Original report