eqsubi15
Ultimate seasoned veteran member
- Joined
- Jul 11, 2020
- RedCents
- 953¢
I'm still trying to learn Lua and be able to build scripts. I haven't been too successful so far. However, one of the things that I believe is needed is some kind of dumbed down explanation of how a Lua script works and what each line exactly does. So, with the help of ChatGPT, I think this might be useful to many.
Used the Aten Ra Mission LEM as the test subject:
[CODE lang="Lua" title="Text Event: Run away with silence emote"]local mq = require('mq')
local function event_handler(line, target)
if not mq.TLO.Zone.ShortName() == 'vexthaltwo_mission' then return end
local my_class = mq.TLO.Me.Class.ShortName()
local i_am_ma = mq.TLO.Group.Member(0).MainAssist()
local my_name = mq.TLO.Me.CleanName()
local ma_name = mq.TLO.Group.MainAssist.CleanName()
if not i_am_ma and (target == my_name or target == ma_name) then
if my_class == 'BER' and mq.TLO.Me.ActiveDisc.Name() == mq.TLO.Spell('Frenzied Resolve Discipline').RankName() then
mq.cmd('/stopdisc')
end
mq.cmdf('/%s mode 0', my_class)
mq.cmd('/mqp on')
mq.cmd('/twist off')
mq.cmd('/timed 5 /afollow off')
mq.cmd('/nav stop')
mq.cmd('/target clear')
mq.delay(100)
mq.cmd('/nav locxyz 1222.67 -48.97 236.41')
mq.delay(15000)
mq.cmdf('/%s mode 2', my_class)
mq.cmd('/mqp off')
mq.cmd('/twist on')
end
end
return {eventfunc=event_handler}[/CODE]
The ChatGPT explanation of each line of the script:
Used the Aten Ra Mission LEM as the test subject:
[CODE lang="Lua" title="Text Event: Run away with silence emote"]local mq = require('mq')
local function event_handler(line, target)
if not mq.TLO.Zone.ShortName() == 'vexthaltwo_mission' then return end
local my_class = mq.TLO.Me.Class.ShortName()
local i_am_ma = mq.TLO.Group.Member(0).MainAssist()
local my_name = mq.TLO.Me.CleanName()
local ma_name = mq.TLO.Group.MainAssist.CleanName()
if not i_am_ma and (target == my_name or target == ma_name) then
if my_class == 'BER' and mq.TLO.Me.ActiveDisc.Name() == mq.TLO.Spell('Frenzied Resolve Discipline').RankName() then
mq.cmd('/stopdisc')
end
mq.cmdf('/%s mode 0', my_class)
mq.cmd('/mqp on')
mq.cmd('/twist off')
mq.cmd('/timed 5 /afollow off')
mq.cmd('/nav stop')
mq.cmd('/target clear')
mq.delay(100)
mq.cmd('/nav locxyz 1222.67 -48.97 236.41')
mq.delay(15000)
mq.cmdf('/%s mode 2', my_class)
mq.cmd('/mqp off')
mq.cmd('/twist on')
end
end
return {eventfunc=event_handler}[/CODE]
The ChatGPT explanation of each line of the script:
1. local mq = require('mq')
- Imports the MacroQuest library (mq) into the script, giving access to its functions and objects.
2. local function event_handler(line, target)
- Defines a function named event_handler that takes two parameters:
- line: Likely the full text of an event or trigger line.
- target: The specific entity involved in the event.
3. if not mq.TLO.Zone.ShortName() == 'vexthaltwo_mission' then return end
- Checks if the player is not in the zone with the short name vexthaltwo_mission. If true, the function exits early (return).
4. local my_class = mq.TLO.Me.Class.ShortName()
- Retrieves the player's class short name (e.g., BER for Berserker) and stores it in my_class.
5. local i_am_ma = mq.TLO.Group.Member(0).MainAssist()
- Checks if the player is the group's main assist and stores the result (true/false) in i_am_ma.
6. local my_name = mq.TLO.Me.CleanName()
- Retrieves the player's clean name (stripped of special characters or titles) and stores it in my_name.
7. local ma_name = mq.TLO.Group.MainAssist.CleanName()
- Retrieves the clean name of the group's main assist and stores it in ma_name.
8. if not i_am_ma and (target == my_name or target == ma_name) then
- Proceeds only if:
- The player is not the main assist (not i_am_ma), and
- The target matches either the player’s name or the main assist's name.
9. if my_class == 'BER' and mq.TLO.Me.ActiveDisc.Name() == mq.TLO.Spell('Frenzied Resolve Discipline').RankName() then
- If the player’s class is Berserker (BER) andthey are currently using the discipline Frenzied Resolve Discipline:
- Executes the block inside this if.
10. mq.cmd('/stopdisc')
- Stops the active discipline (e.g., Frenzied Resolve Discipline).
Actions Taken Regardless of Class:
The following commands are executed if the conditions of the outer if statement are met:11. mq.cmdf('/%s mode 0', my_class)
- Sends a formatted command to set the player’s class mode to 0 (passive mode). %s is replaced with the player’s class name.
12. mq.cmd('/mqp on')
- Enables /mqp, which prevents queuing additional commands.
13. mq.cmd('/twist off')
- Turns off twisting (used for bards to cycle songs).
14. mq.cmd('/timed 5 /afollow off')
- Schedules the command /afollow off to run after 5 seconds.
15. mq.cmd('/nav stop')
- Stops navigation or pathing.
16. mq.cmd('/target clear')
- Clears the current target.
17. mq.delay(100)
- Pauses the script for 100 milliseconds.
18. mq.cmd('/nav locxyz 1222.67 -48.97 236.41')
- Navigates the player to the specific location (1222.67, -48.97, 236.41).
19. mq.delay(15000)
- Pauses the script for 15 seconds, likely to wait for movement or an event.
20. mq.cmdf('/%s mode 2', my_class)
- Resumes the player’s class mode to 2 (active or combat mode).
21. mq.cmd('/mqp off')
- Disables /mqp, allowing normal command queuing.
22. mq.cmd('/twist on')
- Turns twisting back on.
23. end (Closing Braces)
- Ends the if block and concludes the main logic of the function.
24. return {eventfunc=event_handler}
- Returns a table (dictionary) that assigns the event_handler function to the key eventfunc. This makes it accessible to MacroQuest as an event handler.


