Lua Triggers
Use Lua scripts after fast MUD-line matching.
Lua triggers run in a browser worker. PlayMUD.online first checks the configured patterns, then runs the Lua script when their matching conditions are met. Normal triggers still run first.
Where To Find Them
Open Automation and click Add Lua Trigger. Lua triggers can be placed in folders with other automation items.
A Lua trigger must compile before it can be enabled. If a parent folder is disabled, the trigger will not run even if the trigger itself is checked.
Match Modes
Incoming text is ANSI-stripped before matching. Choose the cheapest mode that fits the job.
containsmatches text anywhere in the line.starts withmatches the beginning of the line.ends withmatches the end of the line.exactmatches the whole trimmed line.regexenables JavaScript regular expressions and capture groups.
The case toggle controls whether text matching and regex matching are case-sensitive.
Multiple Patterns
In the Lua trigger editor, use Add pattern for more rows. Each row has its own match type. Use the arrow buttons to reorder rows or Remove to delete one. Existing triggers start with their original single pattern.
- Any pattern (OR) runs the script when any row matches. If several match the same incoming line, the first matching row supplies the captures and the script runs once.
- All patterns in order (AND) waits for every row to match in its listed order. Line window is the maximum number of incoming lines after the first match in which the remaining rows must match. Blank lines count.
For example, a window of 5 permits the remaining patterns on the starting line or any of the next five lines. Rows can match on the same line; a window of 0 requires that. The window can be 0–1000, and a trigger can contain up to 50 pattern rows.
This follows the Any/All pattern approach described in Mudlet's trigger documentation. Regex rows use PlayMUD.online's existing JavaScript regex syntax. The case-sensitive setting applies to every row.
Try these two regex rows in All patterns in order mode with a window of 3:
1. ^You begin casting (.+)\.$
2. ^Your spell hits for (\d+) damage\.$
Then use this Lua script:
local spell = multimatches[1][2]
local damage = multimatches[2][2]
echo(spell .. " dealt " .. damage .. " damage")
multimatches[row][1] is that row's full match and multimatches[row][2] its first regex capture. matchedLines[row] is the original line text. matches, line, raw, highlighting, replacement, and gagging refer to the completing line. In Any mode, patternIndex identifies the matching row and multimatches[1] contains its captures.
Use the multiline test box to paste sample output before enabling the trigger. The tester reports the first completed match or the stage it is waiting for. It does not run the script.
Partial sequences expire when their window runs out. Loading settings, reconnecting, disabling a trigger or its folder, or changing its patterns or script clears its partial matches. When starts overlap at the same stage, the newer start supplies the captures. A completed sequence is consumed even if cooldown prevents its script from running.
First Example
This trigger watches for a line like You receive 42 gold and prints a small colored note.
Pattern: ^You receive (\d+) gold
Match: regex
local amount = matches[2]
echo("<gold>Gold gained:<reset> " .. amount)
matches[1] is the complete matched text (which can be part of a line for regex patterns). matches[2] is the first captured part, which is the number inside (\d+). The word local means this temporary value is only used inside this run. The .. joins text together.
Line And Matches
Every Lua trigger receives two useful values: line, which is the current MUD line, and matches, which holds the matched text pieces.
echo("The full line was: " .. line)
echo("The matched text was: " .. matches[1])
Variables
Use vars.name for saved variables. Saved variables can be used later in normal commands with braces, such as {target}.
vars.target = "orc"
echo("Target is " .. vars.target)
This stores orc in a saved variable named target. Later, a normal command like kill {target} becomes kill orc.
Use normal command variables as {target} outside Lua.
Temporary State
Use state.name for values that should last while this browser session is active, but do not need to be saved with your profile.
state.last_gold_line = line
state.gold_seen = (state.gold_seen or 0) + 1
echo("Gold lines seen: " .. state.gold_seen)
The expression state.gold_seen or 0 means "use the old count if it exists, otherwise start at zero."
Actions
send("look")
send("look", 1) -- silent local echo
echo("<green>Green text<reset> normal text")
echo("<#12FA00>TrueColor<reset>")
echo("<bg:#202000>background<reset>")
gag()
sub("<green>Replacement line<reset>")
highlight("yellow")
highlight("white", "dark_blue")
timer(5, [[ echo("<green>Five seconds later<reset>") ]])
echo() output does not launch triggers. gag() removes the matched MUD line. sub(text) removes the matched line and inserts formatted echo-style text in its place.
Echo colors persist between echo-style calls during one Lua event, then reset when the script ends. See the Echo Colors reference for names and TrueColor syntax.
timer(seconds, luaSource) schedules a one-shot browser-side Lua run. It returns an id that can be passed to killTimer(id). Use remainingTime(id) to check seconds left for an active timer.
Allowed Runtime
The Lua environment exposes common pure Lua helpers, plus string, table, math, and os.time(). Browser and system APIs such as require, package, io, debug, DOM access, fetch, and storage are not exposed to user scripts.
Live trigger runs have a CPU watchdog. If a script hangs, PlayMUD.online terminates that worker, disables only that Lua trigger, and writes the timeout to the Activity tab.
PlayMUD.online limits sends, echoes, highlights, stored values, and Lua run time. The Lua timeout does not cover regex matching; see Pattern Performance.
Because actions are returned from the worker after the script finishes, send(), echo(), gag(), sub(), and highlight() are applied in script order after a successful run.