I try understanding it but as much as i try i just can’t make sense of it, especially the part with th metatables. How do you use them?
Yes I have, it did not help me much, what does it meand by index / indexed? Also are the metamethods the only thing that can be stored in a metatable?
Have you ever seen the error? Thing attempted to Index nil with ":FindFirstChild()" -- Something along these lines.
basically __index
provides a backup solution in a table when it trys to index nil. Lets say you tried to find a nil value and you set the __index to return false
. It will return false.
In addition plenty of tutorials exist on the lovely platform Youtube.
Here is 1 notable tutorial by AlvinBlox. METATABLES - Roblox Scripting Tutorial - YouTube
So __index fires when a nonextisting value is called for in a table? And then you can set it to return a value?
Yes, Heres an example.
local Service = setmetatable({ }, {
__index = function(table,value)
return game:GetService(value)
end
})
print(Service.ReplicatedStorage)
´´´lua
local Vector3 = {}
Vector3.__index = Vector3
function Vector3.new(x, y, z)
local self = setmetatable({}, Vector3)
self.x = x or 0
self.y = y or 0
self.z = z or 0
return self
end
´´´
so in this case Vector3 is a metatable right?
Yes that is correct, Vector3 is the metatable.