Alert Dialog

The Alert dialog system provides modal alert messages with support for different types, confirmation dialogs, and customizable buttons. Perfect for displaying important information and getting user confirmation.

Dark Mode Alert

alert

Display an alert dialog with customizable options and styling.

Parameters:

  • playerId (number, server-side only): Player server ID to send the alert to

  • options (table): Alert dialog configuration options

    • title (string, optional): Alert title (default: 'Alert')

    • message (string, optional): Alert message content (default: empty string)

    • type (string, optional): Alert type - 'info', 'success', 'warning', 'error', 'confirm' (default: 'info')

    • position (string, optional): Dialog position on screen (default: from config)

    • showBackground (boolean, optional): Show background overlay (default: from config)

    • showConfirm (boolean, optional): Show confirm button (default: auto-determined by type)

    • showCancel (boolean, optional): Show cancel button (default: false, true for confirm type)

    • confirmText (string, optional): Confirm button text (default: 'OK' or 'Confirm' for confirm type)

    • cancelText (string, optional): Cancel button text (default: 'Cancel')

    • icon (string, optional): Custom icon name override

    • confirmButtonType (string, optional): Confirm button styling type

    • cancelButtonType (string, optional): Cancel button styling type

    • duration (number, optional): Auto-dismiss duration in milliseconds (default: 5000 for non-interactive)

    • id (string, optional): Custom alert dialog ID (default: auto-generated)

    • size (string, optional): Dialog size - 'small', 'medium', 'large' (default: 'medium')

    • closable (boolean, optional): Whether alert can be closed with X button (default: true)

    • persistent (boolean, optional): Whether alert persists across resource restarts (default: false)

Returns: number|boolean - Alert dialog ID on success, false on failure

Example:

hideAlert

Hide the currently active alert dialog immediately.

Returns: boolean - Success status

Example:

showAlert

Alias for the alert() function providing the same functionality with identical parameters.

Parameters: Same as alert() function

Returns: number|boolean - Alert dialog ID on success, false on failure

Example:

getAlertState

Get the current state of active alert dialogs.

Returns: table - Alert state information

  • active (boolean): Whether an alert dialog is currently active

  • id (string): Current alert dialog ID

  • type (string): Current alert type

  • title (string): Current alert title

  • message (string): Current alert message

  • showConfirm (boolean): Whether confirm button is shown

  • showCancel (boolean): Whether cancel button is shown

  • duration (number): Auto-dismiss duration

  • startTime (number): Alert start timestamp

Example:

alertAll (Server-side only)

Send an alert dialog to all online players simultaneously.

Parameters:

  • options (table): Alert dialog configuration options (same as alert function)

Returns: boolean - Success status

Example:

hideAlertPlayer (Server-side only)

Hide alert dialog for a specific player from server-side.

Parameters:

  • playerId (number): Player server ID

Returns: boolean - Success status

Example:

Alert Types

Info Alerts

Blue-themed alerts for general information and neutral messages.

Success Alerts

Green-themed alerts for positive feedback and successful operations.

Warning Alerts

Orange-themed alerts for warnings, cautions, and important notices.

Error Alerts

Red-themed alerts for errors, failures, and critical issues.

Confirm Alerts

Interactive confirmation dialogs with confirm/cancel buttons for user decisions.

Button Configuration

Custom Button Text and Styling

Show/Hide Button Configuration

Dialog Sizing and Positioning

Size Variants

Position Configuration

Server-Side Usage

Alert Specific Player

Alert All Players

Conditional Server Alerts

Bulk Alert Operations

Event System

The Alert system provides comprehensive event handling for user interactions:

b2lib:alert-response

Triggered when user interacts with alert buttons or when alerts auto-dismiss.

b2lib:alert-shown

Triggered when an alert dialog is displayed.

b2lib:alert-hidden

Triggered when an alert dialog is hidden or dismissed.

Configuration

Configure alert dialog defaults in config.lua:

Advanced Examples

Confirmation with Callback Handling

Multi-Step Confirmation Process

Timed Alerts with Dynamic Content

Custom Alert Types and Styling

Server-Side Event-Driven Alerts

Alert Queue Management

Best Practices

  1. Use Appropriate Types - Match alert type to the message context for better user experience

  2. Keep Messages Clear - Use concise, actionable language that users can quickly understand

  3. Provide Context - Use descriptive titles to set proper expectations for the alert content

  4. Handle Responses Properly - Always listen for alert response events and handle them appropriately

  5. Use Confirmations Wisely - Reserve confirm type for destructive or important actions only

  6. Set Appropriate Durations - Important messages should stay longer, routine messages shorter

  7. Test User Flows - Ensure alert sequences make logical sense to users

  8. Avoid Alert Spam - Don't overwhelm users with too many alerts at once

  9. Consider Accessibility - Ensure alerts work with keyboard navigation and screen readers

  10. Use Server-Side for Critical Alerts - Important alerts should come from server to ensure delivery

Troubleshooting

Alert Not Showing

  1. Check B2Lib Initialization: Verify B2Lib is properly initialized before calling functions

  2. Check Modal Conflicts: Ensure no other modal dialogs are currently active

  3. Validate Parameters: Ensure all required parameters are provided and valid

  4. Check Console Errors: Look for error messages from B2Lib.Debug in the console

  5. Test with Simple Example: Try a basic alert without optional parameters

Buttons Not Working

  1. Verify Event Handlers: Ensure event handlers are registered before showing alerts

  2. Check JavaScript Errors: Look for errors in the browser console (F12)

  3. Test NUI Focus: Ensure NUI focus is properly managed and not blocked

  4. Validate Button Configuration: Check showConfirm and showCancel settings

  5. Test with Minimal Setup: Start with a simple confirmation alert

Auto-dismiss Not Working

  1. Check Duration Setting: Ensure duration is set correctly and is a positive number

  2. Verify Alert Type: Confirm alert type allows auto-dismiss (non-interactive alerts)

  3. Check for Conflicts: Ensure no other code is hiding the alert prematurely

  4. Test Timing: Verify there are no timing conflicts with other UI elements

  5. Monitor State: Use getAlertState() to check alert status

Event Handling Issues

  1. Register Handlers Early: Register event handlers before showing any alerts

  2. Check Handler Logic: Verify event handler functions don't contain errors

  3. Validate Event Data: Ensure event data structure matches expectations

  4. Test Incrementally: Start with minimal event handlers and add complexity

  5. Debug Event Flow: Add logging to track event triggering and handling

Server-Side Issues

  1. Check Player Validity: Ensure player ID is valid before sending alerts

  2. Handle Disconnections: Account for players disconnecting during alert display

  3. Validate Permissions: Check player permissions before sending sensitive alerts

  4. Test Event Registration: Verify server events are properly registered and triggered

  5. Monitor Network Issues: Check for network problems affecting client-server communication

Performance Issues

  1. Limit Concurrent Alerts: Avoid showing multiple alerts simultaneously

  2. Use Alert Queuing: Implement queuing system for multiple alerts

  3. Optimize Event Handlers: Ensure event handlers are efficient and don't block

  4. Monitor Memory Usage: Check for memory leaks with long-running alerts

  5. Test with Multiple Players: Verify performance with many concurrent users

Last updated