Skip to content
MetatablesOverview

hookmetamethod

Represents interception of metamethod behavior.

Details#

Replaces an existing function-valued metamethod and returns its previous implementation. The metamethod must already exist. Shared metatables widen the scope of a hook; a local table keeps this example isolated.

Examples#

Example

Luau
local value = setmetatable({}, { __tostring = function() return "before" end })
local original = hookmetamethod(value, "__tostring", function() return "after" end)
print(tostring(value)) -- after
restorefunction(getrawmetatable(value).__tostring)
print(original()) -- before