Printing Key From Table Returns Nil?

I’m trying to print the new key added into the table

After making a new key and setting the value into the “normal” table and then trying
to print the new value of the key in the table, it prints nil for some reason.

I’m trying to learn meta tables, but this is really confusing and i cant find help.

I thought it was only supposed to say that the “key was added” like i told it to
but clearly its doing something else to the value and making it non existant.

local normal = {}

local meta = { 
    __newindex = function(t, k)
        print(k.." added")
        return "default"

setmetatable(normal, meta)

normal.x = 25

print(normal.x) -- Prints nil

How __newindex works is that instead of assigning the new index, it calls the function given instead. If you still want it to be added then use rawset:

local meta = { 
    __newindex = function(t, k, v)
        rawset(t, k, v) -- newindex passes the value too
        print(k.." added")
        -- i dont know if returning a value even does anything
1 Like

That makes way more sense, thank you.