Closures
Hook, wrap, clone and inspect functions.
Functions are the unit of behavior in Luau, and these are the tools for changing it. Hooking intercepts a function everywhere it's called. The rest help your hooks behave exactly like the functions they replace.
Hooking
hookfunction
since 1.0.0hookfunction(target: function, hook: function): functionReplaces target with hook and returns a new function that calls the original.
The replacement happens in place: every existing reference to target, including references held by game scripts, now runs hook.
Also available as replaceclosure.
Parameters
targetfunction- The function to hook. Luau and C functions are both supported.
hookfunction- The function to run instead. When
targetis a C function, wraphookinnewcclosure.
Returns
functionA function that calls the original, unhooked target.
Example
hookmetamethod
since 1.0.0hookmetamethod(object: any, method: string, hook: function): functionHooks the metamethod method on object's metatable and returns the original.
Every Instance shares one metatable, so hooking __namecall or __index on game affects every Instance in the game.
Parameters
objectany- Any value with a metatable. Usually
game. methodstring- The metamethod, such as
"__namecall","__index"or"__newindex". hookfunction- Receives the same arguments the metamethod would.
Returns
functionThe original metamethod.
Example
newcclosure
since 1.0.0newcclosure(fn: function): functionWraps a Luau function in a C closure. The wrapper behaves exactly like fn, but reports itself as a C function to iscclosure, debug.info and stack traces. It can yield.
Parameters
fnfunction- The Luau function to wrap.
Returns
functionA C closure that calls fn.
Example
Inspection
iscclosure
since 1.0.0iscclosure(fn: function): booleanReturns whether fn is a C closure: a Roblox or Lynx built-in, or a function wrapped with newcclosure.
Parameters
fnfunction- The function to check.
Returns
booleantrue for C closures.
Example
islclosure
since 1.0.0islclosure(fn: function): booleanReturns whether fn is a Luau closure. The exact opposite of iscclosure.
Parameters
fnfunction- The function to check.
Returns
booleantrue for Luau closures.
Example
isexecutorclosure
since 1.2.0isexecutorclosure(fn: function): booleanReturns whether fn belongs to Lynx: defined in a script you executed, or a Lynx built-in. Functions from game scripts and Roblox built-ins return false.
Also available as checkclosure and isourclosure.
Parameters
fnfunction- The function to check.
Returns
booleantrue for Lynx functions.
Example
checkcaller
since 1.0.0checkcaller(): booleanReturns true when the current function was called from a Lynx thread, and false when a game script called it. Inside a hook, this is how you tell your own calls from the game's.
Returns
booleantrue when called from code you executed.
Example
getcallingscript
since 1.1.0getcallingscript(): BaseScript?Returns the script that called the current function, or nil when the caller is Lynx. Use it in hooks to see which game script triggered a call.
Returns
BaseScript?The calling script.
Example
Copying
clonefunction
since 1.1.0clonefunction(fn: function): functionReturns a copy of fn with the same environment, upvalues and behavior. Hooks applied to fn later don't affect the copy, which makes it a reliable way to keep a clean reference.
Parameters
fnfunction- The function to copy.
Returns
functionAn independent copy of fn.
Example
Written for Lynx 0.0.0
Something unclear? Tell us on Discord