Your Second Lua Plugin
The Golden Snitch Detector
Section titled “The Golden Snitch Detector”Let’s build a plugin that monitors all Lua code being loaded on the server and alerts you when it contains the text “the golden snitch”.
This introduces event handling with Autorun.on() and the powerful loadbuffer event.
Create your plugin structure:
Directorysnitch_detector
Directorysrc
Directoryclient
- init.lua
- plugin.toml
[plugin]name = "snitch_detector"author = "you"version = "0.1.0"description = "Detects the golden snitch in loaded code"language = "lua"The loadbuffer Event
Section titled “The loadbuffer Event”The loadbuffer event fires whenever Lua code is about to be loaded and executed. This includes addon code, console commands, RunString calls - everything.
See the Events reference for full parameter details.
Implementation
Section titled “Implementation”Autorun.on("loadbuffer", function(name, content, mode) if string.find(content, "the golden snitch") then Autorun.print("Found the golden snitch in: " .. name) endend)That’s it. The event handler receives every piece of code loaded on the server and checks if it contains our target string.
Going Further
Section titled “Going Further”Counting Occurrences
Section titled “Counting Occurrences”local snitchCount = 0
Autorun.on("loadbuffer", function(name, content, mode) if string.find(content, "the golden snitch") then snitchCount = snitchCount + 1 Autorun.print("Snitch #" .. snitchCount .. " found in: " .. name) endend)Logging to File
Section titled “Logging to File”Autorun.on("loadbuffer", function(name, content, mode) if string.find(content, "the golden snitch") then local timestamp = os.date("%Y-%m-%d %H:%M:%S") local log = string.format("[%s] Found in: %s\n", timestamp, name)
-- Append to log file Autorun.append("snitches.txt", log)
Autorun.print("Logged snitch detection: " .. name) endend)Performance Considerations
Section titled “Performance Considerations”The loadbuffer event fires frequently. Keep your handlers fast:
Autorun.on("loadbuffer", function(name, content, mode) -- Don't do expensive operations on every single chunk for i = 1, 1000000 do -- Heavy processing endend)Autorun.on("loadbuffer", function(name, content, mode) -- Quick string check, bail early if no match if not string.find(content, "the golden snitch") then return end
-- Only do work when needed Autorun.print("Found snitch in: " .. name)end)