hookproto
Hook a nested Luau proto.
Syntax
hookproto(proto: ProtoProxy, hook: (...any) -> ...any) -> ()Parameters
| Parameter | Type | Description |
|---|---|---|
proto | ProtoProxy | Proto to hook |
hook | (...any) -> ...any | New Luau behavior for the proto |
Returns
This function does not return a value.
Description
Use debug.getproto or debug.getprotos to get a ProtoProxy. Pass it to hookproto.
The hook applies to existing and future closures that use the proto. It stays active until you call restoreproto.
hookproto does not return the original function.
Example
local function makeGreeter()
local function greet(name)
return `Hello, {name}!`
end
return greet
end
local greet = makeGreeter()
local proto = debug.getproto(makeGreeter, 1)
hookproto(proto, function(name)
return `Hooked: {name}`
end)
print(greet("Volt")) -- Hooked: Volt
print(makeGreeter()("Docs")) -- Hooked: Docs
restoreproto(proto)
print(greet("Volt")) -- Hello, Volt!Hook Requirements
hookmust be a Luau function. C functions are not accepted.hookmust have zero upvalues. It cannot capture a local variable.
local prefix = "Hooked"
-- Rejected because the function captures prefix.
hookproto(proto, function(value)
return `{prefix}: {value}`
end)Related Functions
restoreproto- Restore a hooked protoisprotohooked- Check if a proto is hookeddebug.getproto- Get one nested protodebug.getprotos- Get every nested proto