In my game, a player can block other players and the PlayFabIds of the blocked players are stored in his ReadOnlyData. The player can also unblock blocked players if he wants. But I don't know how to remove a certain PlayFabId from ReadOnlyData because it is one big string.
For example, how can I remove the PlayFabId 57440D2996790D06 from this ReadOnlyData key/value pair in CloudScript?
Key:
BlockedPlayers
Value(blocked PlayFabIds):
57440D2996790D06,88440D2996790D06,99440D2996790D06
I get the key/value pair like this in CloudScript:
var friendid = args.FriendId; // it is: 57440D2996790D06 var key = "BlockedPlayers"; var blockedplayers = server.GetUserReadOnlyData({PlayFabId: currentPlayerId, Keys: "BlockedPlayers"}); if (blockedplayers.Error == null) { if ((blockedplayers.Data.hasOwnProperty(key)) && (blockedplayers.Data[key].Value != "") && (blockedplayers.Data[key].Value != null)) { var jsondata = blockedplayers.Data[key].Value;
Answer by Made Wang · Jun 27 at 09:15 AM
You need to split this string, delete the specified PlayFabId, and then generate a new string with the remaining contents to update ReadOnlyData. Refer to the code below.
handlers.handleBlockedPlayers=function(args,context){ var unBlockedPlayFabId = args.unBlockedPlayFabId; var key="BlockedPlayers"; var blockedplayers=server.GetUserReadOnlyData( { PlayFabId: currentPlayerId, Keys: key }); if(blockedplayers.Error==null) { if ((blockedplayers.Data.hasOwnProperty(key)) && (blockedplayers.Data[key].Value != "") && (blockedplayers.Data[key].Value != null)) { var players=blockedplayers.Data[key].Value.split(","); if(players.includes(unBlockedPlayFabId)) { var index=players.indexOf(unBlockedPlayFabId); players.splice(index,1); var newPlayers=players.toString(); server.UpdateUserReadOnlyData({ PlayFabId: currentPlayerId, Data: { [key]: newPlayers } }); } } } }