Search code examples
luametatable

Lua - How would I break meta tables or make them unuseable?


So in my game most exploiters are using metatables to stop bans and I would like to break them, I dont use them and it would only hurt the exploiters. Even if this breaks other parts of lua I can and will fix it but this needs to stop and this is my best bet.


Solution

  • From Programming in Lua, 4th Edition, chapter 20:

    The functions setmetatable and getmetatable also use a metafield, in this case to protect metatables. Suppose we want to protect our sets, so that users can neither see nor change their metatables. If we set a __metatable field in the metatable, getmetatable will return the value of this field, whereas setmetatable will raise an error:

    mt.__metatable = "not your business"
    
    s1 = Set.new{}
    print(getmetatable(s1))     --> not your business
    setmetatable(s1, {})
        stdin:1: cannot change protected metatable
    

    So, your answer? Set the __metatable field to prevent access to a table’s metatable. Do that for all tables that you need to protect.