setrawmetatable
Sets the raw metatable of an object.
Syntax
setrawmetatable(object: any, metatable: table?) -> voidParameters
| Parameter | Type | Description |
|---|---|---|
object | any | The object to modify |
metatable | table? | The new metatable, or nil to remove |
Returns
This function does not return a value.
Description
setrawmetatable sets the metatable of an object directly, bypassing normal restrictions.
Example
local myTable = {}
-- Create a custom metatable
local mt = {
__index = function(self, key)
return "Key not found: " .. key
end
}
setrawmetatable(myTable, mt)
print(myTable.anything) -- "Key not found: anything"Replacing Metatables
local object = {}
local originalMt = getrawmetatable(object)
-- Set a new metatable
setrawmetatable(object, {
__tostring = function()
return "Custom string!"
end
})
print(tostring(object)) -- "Custom string!"
-- Restore original
setrawmetatable(object, originalMt)Caution
Modifying metatables of Roblox objects can cause unexpected behavior or crashes. Always keep a reference to the original.
Related Functions
getrawmetatable- Get a raw metatable