I'm currently developing a game in Unity and using PlayFab for backend services. I'm trying to retrieve player display names from a specific segment using PlayFab CloudScript, but I'm encountering issues with the implementation. Here's the CloudScript function I'm using:
handlers.GetPlayersInSegment = function (args, context) {
var segmentId = "BD37826336FD4711"; // My actual segment ID
var playerDisplayNames = [];
var segmentMembers = server.GetPlayersInSegment({ SegmentId: segmentId }).Profiles;
var limit = Math.min(20, segmentMembers.length);
for (var i = 0; i < limit; i++) {
var profile = segmentMembers[i];
playerDisplayNames.push(profile.DisplayName);
}
return { playerDisplayNames: playerDisplayNames };
};
In my Unity C# script, I'm calling this CloudScript function using PlayFabClientAPI.ExecuteCloudScript, but I'm getting a "Failed to retrieve player display names. FunctionResult is null" error in the OnCloudScriptSuccess callback.
Here's the relevant C# code:
private void SearchForPlayer()
{
string segmentId = "BD37826336FD4711"; // My actual segment ID
PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest
{
FunctionName = "GetPlayersInSegment",
FunctionParameter = new Dictionary<string, object> { { "segmentId", segmentId } }
}, OnCloudScriptSuccess, OnCloudScriptFailure);
}
private void OnCloudScriptSuccess(ExecuteCloudScriptResult result)
{
var functionResult = result.FunctionResult as Dictionary<string, object>;
if (functionResult != null && functionResult.ContainsKey("playerDisplayNames"))
{
var displayNames = functionResult["playerDisplayNames"] as List<object>;
List<string> playerNames = new List<string>();
foreach (var displayName in displayNames)
{
playerNames.Add(displayName.ToString());
}
Debug.Log("Player Display Names: " + string.Join(", ", playerNames));
}
else
{
Debug.LogError("Failed to retrieve player display names.");
}
}
private void OnCloudScriptFailure(PlayFabError error)
{
Debug.LogError("CloudScript call failed: " + error.ErrorMessage);
}
I've followed the PlayFab documentation and examples, but I can't seem to get it working. Any help or suggestions on what might be causing this issue would be greatly appreciated. Thank you.