Skip to content

Your Second Lua Plugin

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 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.

Autorun.on("loadbuffer", function(name, content, mode)
if string.find(content, "the golden snitch") then
Autorun.print("Found the golden snitch in: " .. name)
end
end)

That’s it. The event handler receives every piece of code loaded on the server and checks if it contains our target string.

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)
end
end)
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)
end
end)

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
end
end)