You can read this or this but metatables is what you need to know. I can give you simple explanation of how they work. First you need to know setmetatable function, it's a function that accepts 2 arguments, table and.. table where the first one is the one you want to set metatable to and the second one is the actual metatable. So you would do this
local MyTable = {}
local MyMetatable = {}
setmetatable(MyTable, MyMetatable)
getmetatable(MyTable)
Now you have a metatable, but it's useless, that's why you use metamethods, the list of them is in the middle of this page, the most used metamethod is probably __index, here is how it works:
local MyTable = {}
local MyMetatable = {
__index = print
}
setmetatable(MyTable, MyMetatable)
MyTable.NilValue('wideseal')
This will print because __index in this case refers to print, why? Because this is built in metamethod and it fires when you index table which MUST be connected to the metatable with the metamethod. __index is explained in roblox debhub like this: Fires when table[index] is indexed, if table[index] is nil. You see, if you index the table which is connected to metatable then your table will be connected to all the metamethods that the metatable it is connected to has so in our case it's only __index, this metamethod fires when indexed table key is nil, in our case i fired NilValue which is not a valid member of the table but since i indexed nil value, __index fired and in my case it returned print function as i set it to it. If you index non-nil value, nothing will happen:
local MyTable = {}
local MyMetatable = {}
function MyTable.test()
print('check')
end
MyMetatable.__index = MyMetatable.test
setmetatable(MyTable, MyMetatable)
MyTable.test()
MyTable.NilValue()
Both of these print check, because test is not nil and we set __index to the test function, that way when you call a nil value, __index refers to MyMetatable.test so both the functions are the same. This is why you often see people doing:
MyMetatable.__index = MyMetatable
That way let's say you have some profile
local MyProfile = {
Cash = 500,
}
You create a new function which will create you a new profile, let's call it new:
local ProfilerMaker = {}
ProfilerMaker.__index = ProfilerMaker
function ProfileMaker.new(Name)
local NewProfile = {
Cash = 500,
Name = Name,
}
return setmetatable(NewProfile, ProfileMaker)
end
function ProfileMaker:Rob()
self.Cash /= 2
print(self.Name .. ' has been robbed!')
end
local MyProfile = ProfileMaker.new('kirda')
print(MyProfile.Name)
print(MyProfile.Cash)
MyProfile:Rob()
print(MyProfile.Cash)
print(MyProfile.new == ProfileMaker.new)
Now this may seem compicated but the last can help you understand it, in function new it created new table and set it's metatable to ProfileMaker, that way NewProfile table is connected to all metamethods of ProfileMaker which there is only one __index, NewProfile has it's own stats, Name and Cash which are the default values in this case. You see i printed MyProfile.Name and it returned kirda because it's apart of the table that new function returns but then i called :Rob() function which is NOT apart of the NewProfile BUT it's apart of the ProfileMaker and since there is __index and i indexed nil value, it will lead to the __index value which in this case is ProfileMaker so it will look for Rob function inside of the ProfileMaker which it exists. That way you can call all functions inside of ProfileMaker but what is self?
In OOP self is very important, i mean... no self no OOP, self = this in other languages if i am sure but no idea. self refers to the table itself and in the function Rob the self was MyProfile because i called it with : which puts self automatically, let's say those three methods are almost the same (except the last one):
workspace.Destroy(workspace)
workspace:Destroy()
workspace.Destroy(Instance.new('Part'))
That might be confusing but if you would call MyProfile.Rob() then self would refer to the first argument you give you could do MyProfile.Rob(MyProfile) but you won't because that's the point of OOP, you can always not use OOP and get good results but OOP is just very comfortable to use.
Sorry the post got too long but here are sources you can read:
OOP1
OOP2
metamethods list
self
1 comment
This is a good article to begin with. https://devforum.roblox.com/t/all-about-object-oriented-programming/8585