Making a "Ban time remaning" and "Ban reason" text boxes

I am making a VR game. I have a scene that the player automatically goes to if they are banned (PlayFab.) I also have two Text Mesh Pro text boxes in the scene. I am trying to make a script that will put the time remaining on the player’s ban in one, and the reason for the ban in the other. I know it is possible, but I am pretty new around here and it is a bit confusing since the player can’t log into PlayFab (Because they are banned.) Here is the script that I currently have, but it does not function. Do you guys have any suggestions or know where I can go to get more help?

Here is the current script I have:

using System;
using System.Globalization;
using UnityEngine;
using PlayFab;
using TMPro;
using Photon.VR;

public class BanManager : MonoBehaviour
{
    [Header("BAN CONFIGURATION")]
    public GameObject[] StuffToDisable;
    public GameObject[] StuffToEnable;
    public MeshRenderer[] StuffToMaterialChange;
    public Material MaterialToChangeToo;
    public TextMeshPro[] BanTimes;
    public TextMeshPro[] BanReasons;

    // This method handles the ban based on the error details returned from PlayFab
    public void HandleBan(PlayFabError error)
    {
        if (error.Error == PlayFabErrorCode.AccountBanned)
        {
            // Disconnect the player
            PhotonVRManager.Manager.Disconnect();

            // Disable specific game objects
            foreach (GameObject obj in StuffToDisable)
            {
                obj.SetActive(false);
            }

            // Enable other game objects
            foreach (GameObject obj in StuffToEnable)
            {
                obj.SetActive(true);
            }

            // Change the material of specific objects
            foreach (MeshRenderer rend in StuffToMaterialChange)
            {
                rend.material = MaterialToChangeToo;
            }

            // Update ban time and reason in the UI
            foreach (var item in error.ErrorDetails)
            {
                UpdateBanTime(item.Value[0]); // Update the time left for the ban
                UpdateBanReason(item.Key);    // Display the ban reason
            }
        }
    }

    private void UpdateBanTime(string playFabTime)
    {
        foreach (TextMeshPro BanTime in BanTimes)
        {
            if (playFabTime == "Indefinite")
            {
                BanTime.text = "Permanent Ban";
            }
            else
            {
                try
                {
                    DateTime unityTime = DateTime.ParseExact(playFabTime, "yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture);
                    TimeSpan timeLeft = unityTime.Subtract(DateTime.UtcNow);
                    int hoursLeft = (int)timeLeft.TotalHours;
                    BanTime.text = string.Format("Hours Left: {0}", hoursLeft);
                }
                catch (FormatException ex)
                {
                    Debug.LogErrorFormat("Failed to parse PlayFab time '{0}': {1}", playFabTime, ex.Message);
                }
            }
        }
    }

    private void UpdateBanReason(string reason)
    {
        foreach (TextMeshPro BanReason in BanReasons)
        {
            BanReason.text = string.Format("Reason: {0}", reason);
        }
    }
}

That just sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Also, ask yourself how much time you want to spend helping players who behave in a manner that gets them banned… just sayin’! :slight_smile:

Maybe be a bit more specific. But one thing you can do is review your code and simply where possible. It isn’t to make the code more “terse” simply to eliminate code that has no added value and only serves to distort the purpose.

For example your UpdateBanTime accepts a string. This doesn’t change during the process but you process it (repeatedly) in the foreach loop. Ask yourself if it is equal to “Indefinite” the first time how could it be not equal the next time? Similarly with the time. Aren’t you basically assigning “Permanent Ban” or a formatted time string to each text object?

Same thing in UpdateBanReason btw. There is no value in formatting the string inside the foreach since they will always be the same.

I realize this doesn’t solve your issue but we don’t know what the issue is. And reducing unnecessary code is always a benefit.

You need to go to playfab website, click your game, go to title settings, API features, And enable “Allow client to view ban reason and duration”