What is __index how do I use it, and what happens when you set it to a table?
What is __index and what happens when you set it to a table?
1 answer
First, we need to understand what Metables are before we get into the functions:
#What are metatables:
In Lua, metatables are tables that allow tables to manipulate another table. This means they make a table more powerful or have a greater advantage over something. Metatables allow us to accumulate data and change the behaviour of such property to be added. We can alter keys with a value.
#What is __index?
__Index is a function in metatables, which is a hierarchy of Metatables that fires when a table should be indexed. __Index can also return a table.
#Why are metatables useful?
Metatables are useful if you want to hide a table or return something that is not a descendant of the workspace. (Meaning it doesn't exist) you can easily use a metatable.
Here's a simple guide on metatables
Metatables usually introduce OOP. That's another topic yet to be introduced!
#Fixed grammatical errors. Date: February 19, 2021
3 comments
wow really good answer im bookmarking it :)
No problem
Fixed minor errors. Date: Feb,20,21
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
They're part of a metatable. https://www.youtube.com/watch?v=aKe6YncHVAo