Arjuna avatar image
Arjuna asked

Get player ID by name

I have implemented a system where a player can send a friend request to another player by writing to their read-only data (using a cloud script) and waiting for that player to accept, and returning an "accept" message - as described in some of the answers on the forums. Works great!

This is fine in a game when I know the playfabID of the friend I want to add.

But if I only know the player's display name (and not their playfabID), it's hard to write to that player's read-only data. Here's a cloud-script solution that I came up with, but it seems really messy. Is there a better way to do this?

handlers.AddFriendByName = function (args, context) {
    var myID = currentPlayerId;
    var friendName = args.friendName;
    var myName = args.myName;
    //Try to add friend for player ID
    var result = server.AddFriend({ "PlayFabId": myID, "FriendTitleDisplayName": friendName });
    //If fail, return {} meaning friend name was not found
    if (result.apiError != null) {        
        return {};
    //Hack to find friend ID:
    //List all friends and search for the one we just added
    var friendsresult = server.GetFriendsList({ "PlayFabId": myID });    
    var friendID = "";
    if ("Friends" in friendsresult) {
        var friends = friendsresult.Friends;
        for (var i = 0; i < friends.length ; i++) {
            if (friends[i].TitleDisplayName == friendName) {
                friendID = friends[i].FriendPlayFabId;
    if (friendID == "")
        return {};

    //Remove friend from playfab (so that we can send a request instead)
    server.RemoveFriend({ "PlayFabId": myID, "FriendPlayFabId": friendID });
    //Call handler function to create a new friend request using the friend ID
    var requestArgs = { "friendID": friendID, "myName": myName };
    handlers.addFriendRequest(requestArgs, null);

    //return {ID}
    return {ID : friendID}
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Arjuna avatar image Arjuna commented ·

Note: I haven't tested this code, so I'm sure there's sytax errors.

0 Likes 0 ·

1 Answer

brendan avatar image
brendan answered

Actually, I would advise against this design pattern, as it has a few problems. Most critically, this design will fail at scale - take, for example, the scenario where you have someone playing in your game who is well-known, such as a popular YouTube streamer. For any game with a non-trivial player population, you'll likely have hundreds or even thousands of people hitting that person with a friend request. If you're writing all requests to the same key, you'll encounter throttling on writes to the key, since only one player can write to it at a time. But also, since multiple players will be reading and then writing it, they'll be stomping each others' data constantly. Alternately, if you have each player write to a distinct key using their PlayFab ID, the player's data space will rapidly run out of keys.

For a way to implement a two-way confirmed friends system that will work using existing components, have a look at this thread, where we describe an implementation that makes use of the friend tag system:

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Arjuna avatar image Arjuna commented ·

Thanks Brendan! That does sound like a better solution, and cleaner too! As someone on the other link said, it would be really useful to add this to the recipes/tutorials section as there were many solutions on the answer forums, and I wasn't sure which was the right one.

0 Likes 0 ·
brendan avatar image brendan ♦♦ Arjuna commented ·

Yes, we're in the process of a fairly significant documentation update. Once we have that complete, we'll be looking at our samples repo, to see about getting more examples in there.

0 Likes 0 ·
Arjuna avatar image Arjuna commented ·
For these steps, I would still need the friend's playfab ID:
  • Use AddFriend on both players, setting each in the other player's friend list
  • Use SetFriendTags on both players, to tag the players uniquely ("requester"/"requestee", or similar)

Would I still need to call server.GetFriendsList and parse through the results to find this ID (assuming I only knew the name to begin with)

0 Likes 0 ·
brendan avatar image brendan ♦♦ Arjuna commented ·

1) Yes, but you can get a PlayFab ID from any of the other unique identifiers (

2) You would get the friends list to check if you have any outstanding friend requests and process them.

1 Like 1 ·
Arjuna avatar image Arjuna brendan ♦♦ commented ·

Ah I didn't know I could use GetAccountInfo with the other identifiers, thanks! That'll work perfect.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Error rendering WebPanel (widgets/consolidation-widget.ftl): org.hibernate.hql.internal.ast.QuerySyntaxException: AvailableConsolidation is not mapped [from AvailableConsolidation up where up.node = :node]