of     1   

SirTipsAlot
#195560496Sunday, August 07, 2016 5:15 AM GMT

For those who doesn't know what a metatable is, read about its interesting But I have a script, and whenever I call table[4], it prints nil rather than what newindex is supposed to explain, here's my code: local t={1,2,3} local metatable={ __newindex=function(self, index, value) print(value..", why") end } setmetatable(t,metatable) print(t[4]) -- Output: nil game.Players:findFirstChild("Holy_Doge"):WaitForChild("leaderstats").Holiness.Value=math.huge
chimmihc
#195566244Sunday, August 07, 2016 7:30 AM GMT

__newindex is fired when you try to set a new value. You want __index.
Kodran
#195566289Sunday, August 07, 2016 7:31 AM GMT

^^Exactly So if you did this: t[4] = 'whatever' then it would run the code in your __newindex function
Facundo100
#195566813Sunday, August 07, 2016 7:48 AM GMT

It prints nil because there's no 4th key in that table. print(t[4]) == print(nil) In the wiki page for metamethods, it says: EXPRESSION: a.b = c INVOKES: __newindex(a, "b", c) __newindex["b"] = c Where: a -> object, in this case the table that has this metatable. b -> index/key, if using a dictionary then use strings, if using an array then use integers. c -> the value of the key Instead of printing nil, we should write this in the script: t[4] = "I'm a new value" Using the a.b = c expression, we see that: a -> t b -> [4] c -> "I'm a new value" Writing t[4] invokes -> __newindex(a, "b", c) Replacing the abc we get your function: __newindex(self, index, value) So, the script will look like this: local t = {1,2,3} local mt={ __newindex = function(self, index, value) print(value..", but why?") end } setmetatable(t,mt) t[4] = "I'm a new value" -- Output: I'm a new value, but why? print(t[4]) -- Output: nil t[4] = "I'm a new value" -- Output: I'm a new value, but why? Notice how printing t[4] still gives nil? It looks strange because in the line above we wrote t[4] = "I'm a new value", so it should print that string, right? Well... Imagine we have a code like this: local list = {1, 2} print(list[3]) "The code will search through the list for the third index in list, realize it's not there, and return nil. That's totally wrong. What really happens is the code will search through the list for the third index, realize it's not there, and then try to see if there's a metatable attached to the table, returning nil if there isn't one." Knowing this, we see that doing t[4] = "string" ONLY executes the __newindex function, and only that. We didn't actually assign t[4] to the value of "string", but rather called the __newindex function because the 4th index doesn't exist. The script actually looks like this: local t = {1,2,3} local mt={ __newindex = function(self, index, value) print(value..", but why?") end } setmetatable(t,mt) t[4] = "I'm a new value" -- key doesn't exist, calling the __newindex function! print(t[4]) -- the __newindex function didn't set t[4] to the string value, so t[4] has nil value. t[4] = "I'm a new value" -- (Read above), so the key doesn't exist, calling the __newindex function! So, how do we actually set t[4] to the value of "string"? The keyword here is set. We need to use the rawset function. RAWSET: rawset (table, index, value) "Sets the real value of table[index] to value, without invoking any metamethod. table must be a table, index any value different from nil, and value any value." Going back to our function, the first parameter is self, and the object we are referring to is a table. We see that the first parameter of rawset is a table, so our final script will look like this: local t = {1,2,3} local mt={ __newindex = function(self, index, value) print(value..", but why?") rawset(self, index, value) end } setmetatable(t,mt) t[4] = "I'm a new value" -- Output: I'm a new value, but why? print(t[4]) -- Output: I'm a new value t[4] = "I'm a new value" -- Output: nothing, key already set!
Facundo100
#195566933Sunday, August 07, 2016 7:52 AM GMT

Or ye, you could do this: local t={1,2,3} local metatable={ __index=function(self, index, value) print(tostring(value)..", why") end } setmetatable(t,metatable) print(t[4])

    of     1