Live Debug • Inspectors • Runtime Tracking
---------------------------------------------------------------------
-- HookLayer: wraps any function and tracks calls, arguments, errors
---------------------------------------------------------------------
local HookLayer = {}
HookLayer.__index = HookLayer
function HookLayer.new(name: string, fn: (...any) -> ...any)
local self = setmetatable({
name = name,
fn = fn,
calls = 0,
errors = 0,
lastArgs = nil,
lastReturn = nil,
}, HookLayer)
function self:__call(...)
self.calls += 1
self.lastArgs = {...}
local ok, result = pcall(self.fn, ...)
if not ok then
self.errors += 1
warn("[Inspector Error]["..self.name.."]:", result)
else
self.lastReturn = result
end
return result
end
return self
end
---------------------------------------------------------------------
-- Inspector: stores hook-wrapped functions & queries them live
---------------------------------------------------------------------
local Inspector = {}
Inspector.__index = Inspector
function Inspector.new()
return setmetatable({registry = {}}, Inspector)
end
function Inspector:wrap(name: string, fn: (...any) -> ...any)
local hook = HookLayer.new(name, fn)
self.registry[name] = hook
return hook
end
function Inspector:stats(name: string)
local h = self.registry[name]
if not h then return nil end
return {
Calls = h.calls,
Errors = h.errors,
LastArgs = h.lastArgs,
LastReturn = h.lastReturn,
}
end
---------------------------------------------------------------------
-- Example Usage
---------------------------------------------------------------------
local inspector = Inspector.new()
local function compute(a, b)
return a * b + math.random(-2, 2)
end
local safeCompute = inspector:wrap("Compute", compute)
task.spawn(function()
while true do
safeCompute(math.random(1,10), math.random(1,10))
local stats = inspector:stats("Compute")
if stats then
print("Compute Stats =>", stats.Calls, "calls,", stats.Errors, "errors")
end
task.wait(0.2)
end
end)