void RandomSelectAndAnnounce(array aPlayers);


void RandomSelectAndAnnounce(array aPlayers)
{
    int nRandomIndex = Random(GetArraySize(aPlayers));
    string sSelectedPlayer = GetArrayString(aPlayers, nRandomIndex);
    object oBottle = OBJECT_SELF;

    PrintString("Selected player: " + sSelectedPlayer); // Debug message

    // Announce the selected player
    AssignCommand(oBottle, ActionSpeakString(sSelectedPlayer + " has been chosen!"));
}


void main()
{
    object oBottle = OBJECT_SELF; // Assuming this script is attached to the bottle object
    float fRange = 5.0; // Range of 5 meters
    object oPlayer;
    string sPlayerNames = "";
    int i = 1;

    SendMessageToPC(oPlayer,"Bottle script started."); // Debug message

    // 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);
         //   SendMessageToPC(oPlayer,"Added player: " + GetName(oPlayer)); // Debug message
        }
        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("The bottle spins..."));
        DelayCommand(5.0, RandomSelectAndAnnounce(aPlayers)); // Delay of 5 seconds
    }
    else
    {
        SendMessageToPC(oPlayer,"No players found within range."); // Debug message
    }
}




