setreadonly
Sets a table's read-only status.
Syntax
setreadonly(table: table, readonly: boolean) -> voidParameters
| Parameter | Type | Description |
|---|---|---|
table | table | The table to modify |
readonly | boolean | The new read-only status |
Returns
This function does not return a value.
Description
setreadonly controls whether a table can be modified. This is essential for modifying Roblox metatables which are locked by default.
Example
local mt = getrawmetatable(game)
-- Unlock the metatable
setreadonly(mt, false)
-- Now we can modify it
local oldIndex = mt.__index
mt.__index = newcclosure(function(self, key)
print("Accessing:", key)
return oldIndex(self, key)
end)
-- Re-lock for safety
setreadonly(mt, true)Pattern for Safe Modification
local function safeModifyMetatable(object, modifier)
local mt = getrawmetatable(object)
local wasReadonly = isreadonly(mt)
if wasReadonly then
setreadonly(mt, false)
end
modifier(mt)
if wasReadonly then
setreadonly(mt, true)
end
end
safeModifyMetatable(game, function(mt)
-- Your modifications here
end)Notes
- Always re-lock metatables after modification
- Some anti-cheats check for unlocked metatables
- Works on any Lua table, not just metatables
Related Functions
isreadonly- Check read-only statusgetrawmetatable- Get the metatable