question

Super.tt avatar image
Super.tt asked

How to AddMembers to Group?

How to add multiplayer to group with api AddMembers.
Currently, I have groupID and list of playerIDs title, how can i add these players to my group?

I'm try with PlayFabGroupsAPI.AddMembers:

public void _click_AddMember(InputField groupID_MembersID)
    {
        if (string.IsNullOrEmpty(groupID_MembersID.text) || string.IsNullOrWhiteSpace(groupID_MembersID.text) || groupID_MembersID.text.IndexOf("_") == -1)
        {
            Debug.LogError("Invalid input data!!");
            return;
        }


        var temp = groupID_MembersID.text.Split('_');//groupID_MembersID
        var members = temp[1].Split('-');
        List<EntityKey> EntityKey_Members = new List<EntityKey>();


        foreach (var member in members)
        {
            EntityKey_Members.Add(EntityKeyMaker(member, "title_player_account"));
        }


        AddMember(temp[0], EntityKey_Members);
    }


    public void AddMember(string groupID, List<EntityKey> EntityKey_Members)
    {
        var request = new AddMembersRequest() { Group = EntityKeyMaker(groupID), Members = EntityKey_Members };
        PlayFabGroupsAPI.AddMembers(request,
                                    result =>
                                    {
                                        Debug.Log(result.ToJson());


                                    },
                                    error =>
                                    {
                                        Debug.LogError(error.ErrorMessage);


                                    });
    }

with input "groupID_MembersID = E98DC68F783B3D5B_84114E013706ECC9-58B7566C1189A642" but I got a error

Entity title_player_account!84114E013706ECC9 is not a member of group group!E98DC68F783B3D5B.

I also tried with Cloudscripts:

handlers.addMembers = function (args, context) {
    
    var group = { Id: args.GroupId, Type: "group" };
    
    var members = [];
    
    for (var memberID in args.MemberIDs.toString().split(',')) {
        var temp = { Id: memberID, Type: "title_player_account" };
        members.push(temp);
    }
    if (members.length == 0){
        log.debug("***members.length***" + members.length);
        return false;
    }
    try {
        entity.AddMembers({ Group: group, Members: members });
    }
    catch (error) {
        log.error(error);
        return false;
    }
    return true;
}

with input:

{
  "GroupId": "E98DC68F783B3D5B",
  "MemberIDs": "84114E013706ECC9,58B7566C1189A642"
}

it gives me 1 error:

{
    "FunctionResult": null,
    "Logs": null,
    "ExecutionTimeSeconds": 0,
    "MemoryConsumedBytes": 0,
    "APIRequestsIssued": 0,
    "HttpRequestsIssued": 0,
    "Error": {
        "Error": null,
        "Message": "There was a problem running addMembers. Check your arguments and try again.",
        "StackTrace": null
    }
}

Did I miss something? Do you have any ideas?

apis
10 |1200

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

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

As our document -- Groups - Add Members said, only title claimants can add new members to the group, and others must use the group application or invite system to add new members to a group. So, you should not call AddMemebers on the Unity program, unless you use Unity to develop a server. If you used the player's entity token to access the API Groups - Add Members, it would return the error. If you only use Unity to develop clients, we would suggest you call the Entity API AddMembers on the CloudScript.

Besides, the syntax of the JavaScript stipulates that in the “for in” statement, the variable front of the “in” is the keys of the array. Hence, in your code, the member Ids are 0 and 1. It would cause the ProfileDoesNotExist error. You can replace the “for in” statement with the “for of” to get the values of the array. The code could be something like this.

handlers.addMembers = function (args, context) {

    var group = { Id: args.GroupId, Type: "group" };

    var members = [];

    for (var memberID of args.MemberIDs.toString().split(',')) {
        var temp = { Id: memberID, Type: "title_player_account" };
        members.push(temp);
    }
    if (members.length == 0) {
        log.debug("***members.length***" + members.length);
        return false;
    }
    try {
        entity.AddMembers({ Group: group, Members: members });
    }
    catch (error) {
        log.error(error);
        return false;
    }
    return true;
}
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.

Super.tt avatar image Super.tt commented ·

Thanks for your support.

It worked for me

handlers.addMembers = function (args, context) {     var group = { Id: args.GroupId, Type: "group" };     var members = [];     for (var memberID of args.MemberIDs.toString().split(',')) {        var temp = { Id: memberID, Type: "title_player_account" };        members.push(temp);    }    if (members.length == 0) {        log.debug("***members.length***" + members.length);}    try {        entity.AddMembers({ Group: group, Members: members });    }    catch (error) {        log.error(error);}    }
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.