How to use metatables?

Hello devforum people

So I dont understand the concept of metatables, and I want to learn more. I’ve tried watching every video but I can’t seem to describe what it actully dose.

What do I want to achieve?

I want to see if I can use metatables in my new admin commands. But I just don’t understand.

What are SOOO special about metatables?

So this is something that I feel many developers rush into and feel that metatables will unlock the secret sauce of Lua and that there code will just do everything and anything they want it to do but this isn’t the case. However there are some really cool things that you can do with metatables with that being said, but what i am trying to say is metatables is no golden ticket for everything you write. Now with that said i am no expert in the department of metatables so I am going to give you my interpretation of what they do.

Metatables are pretty much a group of methods and functions that can be attached to a table that will alter their functionality to allow you to add different methods and other things which makes doing things given the right circumstances much easier. For example Object Oriented Lua, Metatables makes doing this much easier and neater.

Now I mentioned that you can “alter the functionality of a table or some object” what I mean by this is you can change it’s properties and how it interacts and behaves with the input you give it. I will provide you with some examples here.

For my lighting system I used the __len metamethod which means when you attempt to get the length of something with a metatable with it it’ll return what you have it return in the function.

Group.__len = function(self)
	return (#self.GroupedFixtures)
end

So I have it returning the length of one of the tables in the object. This is an example of altering how the code interacts with the input.

I know metatables can seem like a tall task, but really the secret to learning them is reading the documentation and also trying to use it in your code and spending a couple hours / days just experimenting with them until the concept eventually clicks in your head. Hope that helps.

Metatables are used to modify how tables behave. This can be applied to contexts like OOP or other useful tasks.

For an OOP example:

local Array = {}
Array.__index = Array -- Set the __index to itself

function Array.new() -- Constructor
     local self = { -- Create new array with members
          foo = 0;
     }
     return setmetatable(self, Array) -- Create new object
end

function Array:foo2()
     --Do stuff
end

return Array

This allows you to create your own custom Classes, each time you call Class.new() it creates a new independent object.

Ex)

local Array = require(path)
local test0 = Array.new()
local test1 = Array.new()

test0.foo = 10
print(test0.foo, test1.foo)

Output:

10 0

EDIT:
The reason for setting __index to itself is so that when you set the metatable of self to Array, it’s told to access the module and any functions inside of it.

2 Likes

What is ‘Path’? And what is self used for? (Sorry I am very dumb and do not understand basic lua that much)

That’s just the path to wherever the modulescript is, for example script.Parent.Array.

Do you think setmetatables will do an impact because idk metatables look useless and wierd so I dont think it will change roblox lua that much?

There’s no reason to really use metatables unless you want to. They’re one of the more advanced concepts in Lua and what actually make it a very unique language. They have their use cases, but if you don’t want to use them you don’t have to.

Hmm what I am seeing is that it combines 2 tables? I THINK?

Not exactly, that’s what one metamethod __index does when it’s given a table, it redirects the access of one table to another.

local Array0 = {}
local Array1 = { foo = 0; }
Array1.__index = Array1

local MetaArray = setmetatable(Array0, Array1) -- Set the metatable of Array0 to Array1
print(MetaArray.foo)

Output:

0

OMG i think i get it! it puts something from an already existing table to a blank one!!!

Not exactly either, you can have stuff in that blank table as well and you can access it. But that’s a fundamental use of them.

And this is just one application of one metamethod. I really recommend that you read about this on the Roblox Wiki.

Script:

local Array1 = {" I love roblox"}
local Array2 = {lol=true;}

local meta = setmetatable(Array1, Array2)

print(meta)

Output:

[1] = " I love roblox"
}

You didn’t set __index, we should take this into DMs for further discussion. Please mark this topic as solved as to not clutter the topic page.

Yoo so im reading the wiki and understanding i lov roBlOX

Okay I wil land tahnk u! Ill read and if i dont undrstand i dm u