You can check out this post. Make sure to look through others posts before posting since it’s essentially the same topic. What does table.__index = table do?
ok yeah but like where do i use this in scripting? as in whats the point in using it
also still dont entirely get it thought this would work
local module = {}
module.__index = module
function module.new()
local T = {}
function T:One()
return 2
end
return setmetatable(T, module)
end
function module:one()
return 1
end
return module
__index is a meta method, used along side meta tables for OOP (Object Oriented Programming) for Lua (and Luau/Roblox’s version of Lua). You really won’t use it unless you use OOP, I suggest following this tutorial for OOP on Roblox:
That’s bad, the table’s metatable’s ‘__index’ metamethod is instructing the the table to look within itself for the indexed key, as the key doesn’t exist (is absent) for the table a cycle of table queries occurs.
Yeah, you would just avoid employing a practice I demonstrated, i.e; making the table’s __index or __newindex metamethods point/refer to the table itself.
read through this, not entirely sure if this is how they work but did something like this
local Block = {}
Block.__index = Block
function Block.New()
local blockT = {}
local B = Instance.new("Part")
B.Size = Vector3.new(4,4,4)
blockT["Block"] = B
setmetatable(blockT, Block)
return blockT
end
function Block:Create()
local B = self.Block
B.Parent = workspace
B.Position = Vector3.new(0,0,0)
end
return Block
local mod = game.ServerScriptService.ModuleScript
local BlockMod = require(mod)
local D = BlockMod.New()
D:Create()
Hey I just found something only covering metatables and metamethods (and as I mentioned before __index is a metamethod) I am currently reading it aswell, and it is really doing a great job at explaining, here: