void main()
{
    object oGM = GetLastSpeaker();
    
    if (!GetIsObjectValid(oGM))
    {
        SendMessageToPC(oGM, "DEBUG: No valid speaker found.");
        return;
    }

    SendMessageToPC(oGM, "DEBUG: Script started.");

    object oArea = GetArea(oGM);
    if (!GetIsObjectValid(oArea))
    {
        SendMessageToPC(oGM, "DEBUG: Could not determine player's area.");
        return;
    }

    SendMessageToPC(oGM, "DEBUG: Player's area detected.");

    object oTrap = GetFirstObjectInArea(oArea);
    object oValidTrap = OBJECT_INVALID;
    int nTrapCount = 0;

    // **Loop through all objects in the area using GetNextObjectInArea()**
    while (GetIsObjectValid(oTrap))
    {
        nTrapCount++;
        SendMessageToPC(oGM, "DEBUG: Checking object " + IntToString(nTrapCount) + " - Tag: " + GetTag(oTrap));

        if (GetObjectType(oTrap) == OBJECT_TYPE_PLACEABLE && GetTag(oTrap) == "CAS_PLC_VFXTRAP")
        {
            oValidTrap = oTrap;
            SendMessageToPC(oGM, "DEBUG: Found hazard with correct tag.");
            break; // Stop at the first valid hazard found
        }

        oTrap = GetNextObjectInArea(oArea); // **Get the next object in the area**
    }

    if (nTrapCount == 0)
    {
        SendMessageToPC(oGM, "DEBUG: No objects found in area.");
    }

    if (!GetIsObjectValid(oValidTrap))
    {
        SendMessageToPC(oGM, "No hazard found in this area.");
        return;
    }

    SendMessageToPC(oGM, "DEBUG: Hazard detected. Fetching dialogue node.");

    int nNode = GetSelectedNodeID();
    SendMessageToPC(oGM, "DEBUG: GetSelectedNodeID() returned: " + IntToString(nNode));

    string sDamage;

    switch (nNode)
    {
        case 13: sDamage = "10d6"; break;
        case 14: sDamage = "15d6"; break;
        case 15: sDamage = "20d6"; break;
        case 16: sDamage = "25d6"; break;
        case 17: sDamage = "30d6"; break;
        case 18: sDamage = "35d6"; break;
        case 19: sDamage = "40d6"; break;
        case 20: sDamage = "45d6"; break;
        case 21: sDamage = "50d6"; break;
        default:
            SendMessageToPC(oGM, "Invalid selection (Node ID: " + IntToString(nNode) + "). Please choose a valid option.");
            return;
    }

    SendMessageToPC(oGM, "DEBUG: Setting hazard damage value.");

    SetLocalString(oValidTrap, "sDamage", sDamage);

    SendMessageToPC(oGM, "DEBUG: Retrieved hazard settings. Preparing report.");

    // **Send the final message in a single line to avoid truncation**
    string sReport = "Hazard Updated - Dialogue Node Selected: " + IntToString(nNode) +
                     " - New Damage Value: " + sDamage;

    SendMessageToPC(oGM, sReport);
    SendMessageToPC(oGM, "DEBUG: Script finished successfully.");
}
