Category
Closures
Hook and replace functions, inspect closures, and executor-style helpers.
Pages in this category
- Environment functions:
getgenv,getrenv,getreg,getgc,getconnections,firesignal, read-only tables, identity,Mappings
How hooking works
Every function the sandbox gives you is a small wrapper. Before running the real function, the wrapper checks whether a hook has been set. That is why hookfunction works on print, string.format, mc.getPlayer and so on, even if a script saved a reference to the function earlier.
hookfunction(target, hook)
Replaces target with hook and returns the original function. Alias: replaceclosure.
local oldPrint
oldPrint = hookfunction(print, function(...)
return oldPrint("[hooked]", ...)
end)
print("hello") --> [hooked] hello
- Both arguments must be functions, otherwise an error is raised.
- For a function that the sandbox wrapped (everything it provides), the hook applies everywhere.
- For any other function, the sandbox replaces every reference it can find in tables reachable from your environment (it scans up to 4000 tables). If it finds none, it raises
hookfunction: this function is not reachable from any global table, so it cannot be hooked.
restorefunction(f)
Removes the hook from a wrapped function. Returns true if it did, false if f is not a wrapped function.
ishooked(f)
Returns true if a hook is set on f.
hookmetamethod(object, name, hook)
Replaces a metamethod in the object's metatable and returns the original.
local old
old = hookmetamethod(Vector3.new(), "__tostring", function(v)
return "V3(" .. old(v) .. ")"
end)
It raises an error if the object has no accessible metatable or if the metatable is read-only (frozen).
newcclosure(f)
Returns a wrapper around f that counts as a C closure for iscclosure.
iscclosure(f)
Returns true for functions made by newcclosure, wrapped sandbox functions, and real C functions.
islclosure(f)
The opposite of iscclosure.
isexecutorclosure(f)
Returns true for functions that came from a script (not wrapped sandbox functions and not C functions).
clonefunction(f)
Returns a new function that calls f.