Docs / Closures

Closures

Function creation, hooking, identification, and callstack inspection.

Library#06 of 23 Functions16
newcclosure(f: function) → function
Wraps a Lua function in a C closure — iscclosure returns true. Preferred for anti-tamper detection.
lua
local wrapped = newcclosure(function(x) return x * 2 end)
print(iscclosure(wrapped))  --> true
print(wrapped(21))          --> 42
newlclosure(f: function) → function
Wraps in a Lua closure.
lua
local l = newlclosure(print)
print(islclosure(l))  --> true
wrapclosure(f: function) → function
Generic wrap — picks the appropriate closure type for the input.
lua
local w = wrapclosure(function() return "wrapped" end)
print(w())
clonefunction(f: function) → function
Structurally identical clone with a distinct identity (f ~= clonefunction(f)).
lua
local p2 = clonefunction(print)
print(print == p2)              --> false
print(comparefunctions(print, p2))  --> true
hookfunction(target: function, hook: function) → function
Redirects all calls to target through hook. Returns the original for chained calls.
aliaseshookfunc, replacefunction, replaceclosure
lua
local original
original = hookfunction(print, function(...)
    original("HOOKED:", ...)
end)
print("hi")   --> HOOKED: hi
hookmetamethod(object: any, method: string, hook: function) → function
Hooks a metamethod (__index, __namecall, etc.) on the object's metatable.
lua
local old
old = hookmetamethod(game, "__namecall", function(self, ...)
    if getnamecallmethod() == "Kick" then return end
    return old(self, ...)
end)
iscclosure(f) → boolean
true if f is a C function.
lua
print(iscclosure(print))  --> true
islclosure(f) → boolean
true if f is a Lua function.
lua
print(islclosure(function() end))  --> true
isnewcclosure(f) → boolean
true if f came from newcclosure.
lua
local w = newcclosure(function() end)
print(isnewcclosure(w))  --> true
isexecutorclosure(f) → boolean
true if f is a closure created or wrapped by Velocity.
aliasescheckclosure, isourclosure, iscustomcclosure, istempleclosure, is_synapse_function, issentinelclosure, is_sirhurt_closure, is_protosmasher_closure
lua
print(isexecutorclosure(getgenv))  --> true
print(isexecutorclosure(print))    --> false
comparefunctions(a: function, b: function) → boolean
true if both functions ultimately dispatch to the same target (unwraps clones).
lua
print(comparefunctions(print, clonefunction(print)))  --> true
getfunctionhash(f: function) → string
Stable hash of the function's bytecode.
lua
print(getfunctionhash(function() return 1 end))
isfunctionhooked(f: function) → boolean
true if f currently has an active hookfunction installed.
lua
hookfunction(print, function() end)
print(isfunctionhooked(print))  --> true
restorefunction(f: function)
Removes any hook installed on f.
aliasesrestorefunc
lua
restorefunction(print)
print(isfunctionhooked(print))  --> false
getcallbackvalue(object: any, memberName: string) → function?
Read the current Lua callback assigned to a callback member (e.g. RemoteFunction.OnClientInvoke).
aliasesgetcallbackmember
lua
local rf = game.ReplicatedStorage.MyRemoteFunction
local cb = getcallbackvalue(rf, "OnClientInvoke")
if cb then print(cb()) end
getcallstack() → {table}
Full callstack — each entry has source, func, currentline, name, what.
lua
for i, frame in ipairs(getcallstack()) do
    print(i, frame.source, frame.currentline)
end