question

agniesportsyt avatar image
agniesportsyt asked

Add Friend using friend request with accept/decline option

I was learning to make friends in Unity using PlayFab and i wanted to do it with a friend request confuimation. I have seen many community posts but i found that they all are posted long time ago. I wanted to know whether any such feature has been added or not else is there any other way to do it, if any please explain it with an example code as I am new too coding and PlayFab and I believe that PlayFab community is highly responsive with valuable answers. Thank you in advance.

apisCloudScriptFriends
10 |1200

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

Rick Chen avatar image
Rick Chen answered

Currently adding friends in PlayFab is a one way process. Players can add other players to their own friend list without the needs of other players’ confirmation.

There is a workaround to implement two-way friend confirmation with Cloud Script:

  1. Player 1 selects to send a friend request to Player 2
  2. Use AddFriend on both players, setting each in the other player's friend list
  3. Use SetFriendTags on both players, to tag the players uniquely ("requester"/"requestee", or similar)
  4. Title uses Push Notification (via Cloud Script) and/or Photon Chat to send notification to the other player
  5. A CloudScript example of above is:
handlers.SendFriendRequest
      = function(args, context){ 
      server.AddFriend( 
      { 
      PlayFabId: currentPlayerId, 
      FriendPlayFabId: args.FriendPlayFabId 
      }); 
      server.SetFriendTags({ PlayFabId:
      currentPlayerId, 
      FriendPlayFabId: args.FriendPlayFabId, 
      Tags: ["requestee"] 
      }); server.AddFriend( 
      { 
      PlayFabId: args.FriendPlayFabId, 
      FriendPlayFabId: currentPlayerId 
      }); 
      server.SetFriendTags( 
      { 
      PlayFabId: args.FriendPlayFabId, 
      FriendPlayFabId: currentPlayerId, 
      Tags: ["requester"] 
      }); server.SendPushNotification({ 
      Recipient: args.FriendPlayFabId, 
      Message: "Nice to meet you" 
      });}
  • Player 2 sees the Push, or the Chat, or finds the friend request upon login (using GetFriendsList, checking for the "requester" tag)
  • Player 2 accepts or declines the friend request
  • If Player 2 accepts, either remove the tags from both players or set them to something like "confirmed", a CloudScript example is:
  • handlers.AcceptFriendRequest
          = function(args, context){ 
          server.SetFriendTags({ PlayFabId:
          currentPlayerId, 
          FriendPlayFabId: args.FriendPlayFabId, 
          Tags: ["confirmed"] 
          }); 
          server.SetFriendTags( 
          { 
          PlayFabId: args.FriendPlayFabId, 
          FriendPlayFabId: currentPlayerId, 
          Tags: ["confirmed"] 
          });}
  • If Player 2 declines, remove each player from the other player's friend list, a CloudScript example is:
  • handlers.DenyFriendRequest
          = function(args,context){ server.RemoveFriend({ PlayFabId:
          currentPlayerId, 
          FriendPlayFabId: args.FriendPlayFabId, 
          }); 
          server.RemoveFriend( 
          { 
          PlayFabId: args.FriendPlayFabId, 
          FriendPlayFabId: currentPlayerId, 
          });}

    Please refer to this thread: https://community.playfab.com/questions/370/206712537-Two-way-friend-confirmation-with-Cloud-Script.html

    If you are not familiar with Cloud Script, please go through this document: CloudScript quickstart

5 comments
10 |1200

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

Mohamed Younes avatar image Mohamed Younes commented ·

Is there a way to achieve the above using c# ClientAPI?, as currently AddFriend only add to the local player and there's no clear way to define another PlayFabID.

1 Like 1 ·
Mohamed Younes avatar image Mohamed Younes Mohamed Younes commented ·

Cloud script works perfectly! thanks

0 Likes 0 ·
HackerTester avatar image HackerTester commented ·
@Rick Chen

Hey Please Tell me how the GetFriendWithTag?

0 Likes 0 ·
Rick Chen avatar image Rick Chen ♦ HackerTester commented ·

Currently, there is no such API. You could use the GetFriendsList API to retrieve all friends with their tags, then filter the target tag on client side.

0 Likes 0 ·
Chethan V avatar image Chethan V commented ·

Can you please give the C# commands needed to run these Cloud scripts. Also now the cloud scripts UI under "Automation" of the Playfab account has changed and there aren't any videos which cover this properly at all. Would you please give some resources to counter this problem...I am stuck in this for the past 5 days

0 Likes 0 ·
Mohamed Younes avatar image
Mohamed Younes answered

Is there a way to achieve the above using c# ClientAPI.? currently AddFriend only add friend to the local player and there's no option that takes PlayFabID for the player.

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.

Rick Chen avatar image Rick Chen ♦ commented ·

No, it is not supposed to allow the client to manipulate other players’ friend list.

0 Likes 0 ·
HackerTester avatar image
HackerTester answered

@Rick Chen Hi, But can we use Display Name or Username to do the same thing i dont want my users to fill in the long playfab id please help.

10 |1200

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

HackerTester avatar image
HackerTester answered
@Rick Chen

Please help me i wanted to use Username or DisplayName instead of playfab id and also i wanted two way friend confirmation

// Code That i want some kind of maybe-- 

how can i do it please help @Rick Chen 

handlers.SendFriendRequest = function(args, context){


      server.AddFriend({
      DisplayName: DisplayName,
      FriendDisplayName: args.friendDisplayName});

       server.SetFriendTags({
       DisplayName: DisplayName,
       FriendDisplayName: args.friendDisplayName,
       Tags: ["requestee"]});

       server.AddFriend({
       DisplayName: args.friendDisplayName,
       FriendPlayFabId: currentPlayerId});

       server.SetFriendTags({
       DisplayName: args.friendDisplayName,
       FriendDisplayName: DisplayName,
       Tags: ["requester"]});


       server.SendPushNotification({
       Recipient: args.friendDisplayName,
       Message: "Nice to meet you"});}
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.

Sarah Zhang avatar image Sarah Zhang commented ·

Please navigate to the new thread you posted -- Server.AddFriend with DisplayName. Can we Use DisplayName Instead of PlayFabId - Playfab Community. to discuss this question with us.

0 Likes 0 ·
UmarHyatt avatar image
UmarHyatt answered

please show us Unity code how to call this function and what is args and context here

10 |1200

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

siddhirajrayate741 avatar image
siddhirajrayate741 answered

How to add a friend on both friend list

please tell me.

thanks.

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.

someone avatar image someone commented ·
 handlers.AddFriendFunction = function(args, context) { 
     server.AddFriend({ 
         PlayFabId: currentPlayerId, 
         FriendPlayFabId: args.FriendPlayFabId 
     }); 

just use this code in cloud script and call the function in the engine

0 Likes 0 ·
thebedirhanucak avatar image
thebedirhanucak answered

if anyone make this work im on all ears.

10 |1200

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

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.