What does table._Index = table mean?

As title say pls tell me what :

Table._index = Table

Do ?

__index is a metamethod, which metamethods are used with metatables. If you’ve ever learned about operator overloading which is a concept in some other languages (like C#, for example) it’s pretty similar to that idea.

There’s actually quite a few threads that were already made that are pretty intuitive tutorials as to how metatables/metamethods function.

A technical definition though:

The __index metamethod will fire when you index something in a table that does not exist. You can assign an __index key to either some value, or a function as a handler. The table, along with the index, will be passed as arguments to the function if you assign it to a function rather than a value.

It’s actually pretty cool. Here’s another example using code:

local myMetamethods = {}

--//Assign metamethod
myMetatable.__index = function(table, index)
print("Index of: "..index.." was not found in "..table)
end

--//Create the metatable
local myMetatable = setmetatable({}, myMetamethods)

myMetatable.apples = 2 --//The __index metamethod will not be invoked, as a new index was created in our table.

print(myMetatables.oranges) --//This index does NOT exist in our table, so our metamethod which is attached to this metatable will invoke, causing the function to execute.

Hope this helps a bit. I can clarify anything that seems confusing. The concept of metatables and metamethods is definitely more complex if you don’t have a strong understanding of tables and table manipulation already.

7 Likes

Thanks ! I will learn more about metatables

1 Like

You can learn it from there! : DevHub | Roblox

1 Like

So if the you did a index that dont exist the, table will search for metatable,s _index ! Noice !

Indeed.

So if you index a table that’s attached to a metatable containing the metamethod __index, and that index does not exist in the table itself, and if the __index metamethod points to another table, it’ll search that table for the index instead, to try to find it.

1 Like

Kinda off topic, but this is also how exploiters spoof properties