Microsoft Azure PlayFab logo
    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Add-ons
    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA
  • Runs on PlayFab
  • Pricing
    • Blog
    • Forums
    • Contact us
  • Sign up
  • Sign in
  • Ask a question
  • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges
  • Home /
  • General Discussion /
avatar image
Question by agniesportsyt · Nov 14, 2020 at 10:55 AM · CloudScriptapisFriends

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.

Comment
agniesportsyt
Mohamed Younes
DeathPro

People who like this

3 Show 0
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Rick Chen · Nov 16, 2020 at 06:46 AM

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

    Comment
    Mohamed Younes
    HackerTester
    DeathPro

    People who like this

    3 Show 5 · Share
    10 |1200 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image Mohamed Younes · May 22, 2021 at 03:30 AM 1
    Share

    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.

    avatar image Mohamed Younes Mohamed Younes · May 22, 2021 at 07:56 PM 0
    Share

    Cloud script works perfectly! thanks

    avatar image HackerTester · Jul 28, 2021 at 06:27 AM 0
    Share
    @Rick Chen

    Hey Please Tell me how the GetFriendWithTag?

    avatar image Rick Chen ♦ HackerTester · Jul 28, 2021 at 09:42 AM 0
    Share

    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.

    avatar image Chethan V · Jan 10 at 03:31 PM 0
    Share

    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

    avatar image

    Answer by Mohamed Younes · May 25, 2021 at 01:27 AM

    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.

    Comment
    DeathPro

    People who like this

    1 Show 1 · Share
    10 |1200 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image Rick Chen ♦ · May 25, 2021 at 02:38 AM 0
    Share

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

    avatar image

    Answer by HackerTester · Jul 20, 2021 at 01:47 AM

    @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.

    Comment

    People who like this

    0 Show 0 · Share
    10 |1200 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image

    Answer by HackerTester · Jul 20, 2021 at 01:47 AM

    @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"});}
    Comment

    People who like this

    0 Show 1 · Share
    10 |1200 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image Sarah Zhang · Jul 20, 2021 at 08:34 AM 0
    Share

    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.

    avatar image

    Answer by UmarHyatt · Mar 15 at 12:01 PM

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

    Comment

    People who like this

    0 Show 0 · Share
    10 |1200 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image

    Answer by siddhirajrayate741 · Apr 01 at 05:44 AM

    How to add a friend on both friend list

    please tell me.

    thanks.

    Comment

    People who like this

    0 Show 0 · Share
    10 |1200 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users

    Your answer

    Hint: You can notify a user about this post by typing @username

    Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

    Navigation

    Spaces
  • General Discussion
  • API and SDK Questions
  • Feature Requests
  • PlayStream
  • Bugs
  • Add-on Marketplace
  • LiveOps
  • Follow this Question

    Answers Answers and Comments

    7 People are following this question.

    avatar image avatar image avatar image avatar image avatar image avatar image avatar image

    Related Questions

    Cloud Script Return Value Problem - Null Reference Error 1 Answer

    Where can I find resources for "best practices" using Cloudscript Functions for coding in C#? 1 Answer

    Updating Online / Offline status of player? 1 Answer

    How to access the variables of cloud scripts in Unity's C# script ? 0 Answers

    ExecuteCloudScript Errors - 'ExecuteCloudScriptResult' does not contain a definition 0 Answers

    PlayFab

    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Runs on PlayFab
    • Pricing

    Solutions

    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA

    Engineers

    • Documentation
    • Quickstarts
    • API Reference
    • SDKs
    • Usage Limits

    Resources

    • Forums
    • Contact us
    • Blog
    • Service Health
    • Terms of Service
    • Attribution

    Follow us

    • Facebook
    • Twitter
    • LinkedIn
    • YouTube
    • Sitemap
    • Contact Microsoft
    • Privacy & cookies
    • Terms of use
    • Trademarks
    • Safety & eco
    • About our ads
    • © Microsoft 2020
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Create an article
    • Post an idea
    • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Explore
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges