Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is __index and what happens when you set it to a table?

1 answer

✓ Accepted 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

0

wow really good answer im bookmarking it :)

1

No problem

0

Fixed minor errors. Date: Feb,20,21

This discussion is preserved as it was. New answers, comments, and votes are closed.