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.

alert
Display an alert dialog with customizable options and styling.
Parameters:
playerId(number, server-side only): Player server ID to send the alert tooptions(table): Alert dialog configuration optionstitle(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 overrideconfirmButtonType(string, optional): Confirm button styling typecancelButtonType(string, optional): Cancel button styling typeduration(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 activeid(string): Current alert dialog IDtype(string): Current alert typetitle(string): Current alert titlemessage(string): Current alert messageshowConfirm(boolean): Whether confirm button is shownshowCancel(boolean): Whether cancel button is shownduration(number): Auto-dismiss durationstartTime(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
Use Appropriate Types - Match alert type to the message context for better user experience
Keep Messages Clear - Use concise, actionable language that users can quickly understand
Provide Context - Use descriptive titles to set proper expectations for the alert content
Handle Responses Properly - Always listen for alert response events and handle them appropriately
Use Confirmations Wisely - Reserve confirm type for destructive or important actions only
Set Appropriate Durations - Important messages should stay longer, routine messages shorter
Test User Flows - Ensure alert sequences make logical sense to users
Avoid Alert Spam - Don't overwhelm users with too many alerts at once
Consider Accessibility - Ensure alerts work with keyboard navigation and screen readers
Use Server-Side for Critical Alerts - Important alerts should come from server to ensure delivery
Troubleshooting
Alert Not Showing
Check B2Lib Initialization: Verify B2Lib is properly initialized before calling functions
Check Modal Conflicts: Ensure no other modal dialogs are currently active
Validate Parameters: Ensure all required parameters are provided and valid
Check Console Errors: Look for error messages from B2Lib.Debug in the console
Test with Simple Example: Try a basic alert without optional parameters
Buttons Not Working
Verify Event Handlers: Ensure event handlers are registered before showing alerts
Check JavaScript Errors: Look for errors in the browser console (F12)
Test NUI Focus: Ensure NUI focus is properly managed and not blocked
Validate Button Configuration: Check showConfirm and showCancel settings
Test with Minimal Setup: Start with a simple confirmation alert
Auto-dismiss Not Working
Check Duration Setting: Ensure duration is set correctly and is a positive number
Verify Alert Type: Confirm alert type allows auto-dismiss (non-interactive alerts)
Check for Conflicts: Ensure no other code is hiding the alert prematurely
Test Timing: Verify there are no timing conflicts with other UI elements
Monitor State: Use getAlertState() to check alert status
Event Handling Issues
Register Handlers Early: Register event handlers before showing any alerts
Check Handler Logic: Verify event handler functions don't contain errors
Validate Event Data: Ensure event data structure matches expectations
Test Incrementally: Start with minimal event handlers and add complexity
Debug Event Flow: Add logging to track event triggering and handling
Server-Side Issues
Check Player Validity: Ensure player ID is valid before sending alerts
Handle Disconnections: Account for players disconnecting during alert display
Validate Permissions: Check player permissions before sending sensitive alerts
Test Event Registration: Verify server events are properly registered and triggered
Monitor Network Issues: Check for network problems affecting client-server communication
Performance Issues
Limit Concurrent Alerts: Avoid showing multiple alerts simultaneously
Use Alert Queuing: Implement queuing system for multiple alerts
Optimize Event Handlers: Ensure event handlers are efficient and don't block
Monitor Memory Usage: Check for memory leaks with long-running alerts
Test with Multiple Players: Verify performance with many concurrent users
Last updated