Confused about how Metatables work?

I want to make metatables that whenever something gets inserted the game will refresh the table (right now I’m just using print to see if __newindex will actually work but apparently whenever I try to index this table the table keys become nonexistant).
But for some reason when ever I do that the entire table gets destroyed?
No clue how setmetatable.
Still clueless but I have searched everyone for this and I don’t know what comes else.
Metatables in metatables work right?

What do you mean by refresh the table?, and could you provide the script you are using if possible?

Lets just say it prints something out for now, what I mean by refreshing the table is that there is something in my game’s script that is suppose to recieve a table and it will reprocess it.

What is the specific use case of this Metatable? It sounds like what you’re doing can easily be pulled off without using a Metatable.

Ok so i think the issue (although i don’t have any reference to the specific code you are using ) is you are not using setmetatable, like if i were to do this:

local Table = {}

setmetatable(Table, Table)

function Table.__newindex()
   print('a') -- a would print 

Table[5] = 3

When a table is created it has not metatable and in order to use metamethods you would have to give it a meta table using setmetatable(Table,Table)

This thread goes more in depth into this:

1 Like

i am using metatables correctly btw heres my code

	ThisWindow.CONFIG = {}
setmetatable(ThisWindow.CONFIG, {__newindex = function()
ThisWindow.CONFIG.Icon = ""
ThisWindow.CONFIG.Position = "Preset"
ThisWindow.CONFIG.Name = "Unamed Window"
ThisWindow.CONFIG.Size =,250,0,250)

The thing prints correctly, however all of the tables keys become nil.
so ThisWindow.CONFIG.Size = nil.

note: this is inside a meta table
i have no idea why this doesn’t work?

My question still stands, I’m really betting you’re not needing a metatable for this.
What is this for?

1 Like

This is for my windows system thing.
I want it to print something (for now) whenever a value is changed.
This is the only way I know how to check if a value is changed in a table.

Ah okay, so for debugging? For the most part you can easily just print variables inside of tables without needing to go through all this effort.

Edit: I’m really not getting this.

Edit 2: Oh wait hold on, I kinda see the need for this.

You have to understand metamethods for this, you would have to use the arguments passed to the __newindex(Table,index, Value) to set to a new/different table. Because when you do this ThisWindow.CONFIG.Position = "Preset" it’s not actually setting ThisWindow.CONFIG.Position to anything instead it’s being “sent” to your new index function

Note that when Lua finds a metatable function, it uses that INSTEAD of the default behavior. So ThisWindow.CONFIG.Icon = "" calls the __newindex function but doesn’t actually put a value in the ThisWindow.CONFIG table.

To put the value in the table in addition to your print (without causing an infinite recursion back into __newindex), use rawset:

setmetatable(ThisWindow.CONFIG, {__newindex = function(t, k, v)
    rawset(t, k, v)