void RandomSelectAndAnnounce(array aPlayers);

void RandomSelectAndAnnounce(array aPlayers)
{
    int nRandomIndex = Random(GetArraySize(aPlayers));
    string sSelectedPlayer = GetArrayString(aPlayers, nRandomIndex);
    object oBottle = OBJECT_SELF;
    object oSpinner = GetPlaceableLastClickedBy();
    string sSpinnerName = GetName(oSpinner);


    PrintString("Selected player: " + sSelectedPlayer); // Debug message

    if (GetLocalInt(oBottle, "Rerolls") >= 3)
    {
        AssignCommand(oBottle, ActionSpeakString("No valid targets found!"));
        SetLocalInt(oBottle, "Rerolls", 0);
        return;
    }
    if (sSelectedPlayer == sSpinnerName)
    {
        SetLocalInt(oBottle, "Rerolls", GetLocalInt(oBottle, "Rerolls") + 1);
        RandomSelectAndAnnounce(aPlayers);
        return;
    }

    // Announce the selected player
    AssignCommand(oBottle, ActionSpeakString(sSelectedPlayer + "<cĄ  > has been chosen! " + sSpinnerName + ", tell them if you want a Truth or Dare!</c>"));
    SetLocalInt(oBottle, "Rerolls", 0);

    // Apply paralysis effect to the spinner for one second
    //effect eParalyze = EffectCutsceneParalyze();
    //ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eParalyze, oSpinner, 1.0);
}

void main()
{
    object oBottle = OBJECT_SELF; // Assuming this script is attached to the bottle object
    float fRange = 7.5; // Range of 5 meters
    object oPlayer;
    string sPlayerNames = "";
    int i = 1;
    object oSpinner = GetPlaceableLastClickedBy();
    
     //   effect eParalyze = EffectCutsceneParalyze();
    //ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eParalyze, oSpinner, 1.0);

    // Scan for players within 5 meters and build the player names string
    while ((oPlayer = GetNearestObject(OBJECT_TYPE_CREATURE, oBottle, i)) != OBJECT_INVALID)
    {
        if (GetDistanceBetween(oBottle, oPlayer) <= fRange && GetIsPC(oPlayer))
        {
            if (sPlayerNames != "") sPlayerNames += ";";
            sPlayerNames += GetName(oPlayer);
        }
        i++;
    }

    // Convert the string to an array
    array aPlayers = StringToArray(sPlayerNames, ";");
    SendMessageToPC(oPlayer, "Number of players found: " + IntToString(GetArraySize(aPlayers))); // Debug message

    if (GetArraySize(aPlayers) > 0)
    {
        // Announce the bottle spin
        AssignCommand(oBottle, ActionSpeakString("<cĄ  >The bottle spins...</c>"));
        DelayCommand(2.0, RandomSelectAndAnnounce(aPlayers)); // Delay of 2 seconds
    }
    else
    {
        SendMessageToPC(oPlayer, "No players found within range."); // Debug message
    }
}
