Code Generator | Lua

VIP Message Capture Script for ArizonaRP

This Lua script captures chat messages starting with "[VIP]" and displays them in a menu when the user types "/avr", enhancing the user experience in ArizonaRP on SAMP by organizing important messages.


Empty image or helper icon

Prompt

I am playing ArizonaRP ( SAMP ). I need a script which will put all messages that are starting with "[VIP]" to a menu which will open by  command "/avr"

Answer

Lua Script for ArizonaRP (SAMP)

This document presents a Lua script that captures all messages starting with "[VIP]" and provides a command to display those messages in a menu when the user types "/avr".

Functionality Overview

  1. Message Capture: The script will listen for chat messages and store those that begin with "[VIP]".
  2. Command Trigger: Executing the command "/avr" opens a menu displaying all captured "[VIP]" messages.
  3. Input Handling: Handles cases where there are no messages in the menu.

Lua Script

-- Create a table to store VIP messages
local vipMessages = {}

-- Function to capture VIP messages
-- @param message string: The incoming chat message
-- @param author string: The author of the chat message
-- This function listens to chat messages and stores those that start with "[VIP]"
function OnChatMessage(message, author)
    -- Check if the message starts with "[VIP]"
    if string.sub(message, 1, 5) == "[VIP]" then
        -- Add the message to the vipMessages table
        table.insert(vipMessages, {msg = message, sender = author})
    end
end

-- Function to display VIP messages when the command "/avr" is executed
-- This function creates a menu of all VIP messages
function ShowVipMenu()
    -- Check if there are VIP messages to display    
    if #vipMessages == 0 then
        print("No VIP messages to display.")
        return
    end

    print("***** VIP Messages *****")
    -- Iterate through each VIP message and display it
    for index, messageData in ipairs(vipMessages) do
        print(string.format("%d. [%s]: %s", index, messageData.sender, messageData.msg))
    end
    print("***********************")
end

-- Command handler to open the VIP message menu
-- This function is called when the "/avr" command is triggered
function CommandHandler(command)
    if command == "/avr" then
        ShowVipMenu()
    end
end

Code Explanation

  • Data Structure: We use a table vipMessages to store incoming VIP messages along with their authors.
  • OnChatMessage: This function checks if a message starts with "[VIP]" and adds it to the vipMessages table.
  • ShowVipMenu: This function displays all captured messages or a message indicating there are no VIP messages to show.
  • CommandHandler: Listens for the command "/avr" and calls the function to show the VIP messages.

Usage Example

To integrate this into a SAMP server script:

  1. Ensure the script is added to the relevant event listener for messages.
  2. Bind the OnChatMessage function to the chat message event.
  3. Bind the CommandHandler function to the command execution event.
-- Example binding (pseudo-code, adjust for actual SAMP environment)
AddEvent("OnChat", OnChatMessage)
AddCommand("/avr", CommandHandler)

Conclusion

This Lua script efficiently captures and displays messages starting with "[VIP]", providing a convenient command to view them in-game. For further enhancements and learning about Lua scripting in gaming, consider exploring the courses on the Enterprise DNA Platform for more comprehensive resources and techniques.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This Lua script captures chat messages starting with "[VIP]" and displays them in a menu when the user types "/avr", enhancing the user experience in ArizonaRP on SAMP by organizing important messages.