Prompt
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
- Message Capture: The script will listen for chat messages and store those that begin with "[VIP]".
- Command Trigger: Executing the command "/avr" opens a menu displaying all captured "[VIP]" messages.
- 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:
- Ensure the script is added to the relevant event listener for messages.
- Bind the
OnChatMessage
function to the chat message event. - 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.
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.