What situations should i use __index in modules and stuff

currently learning what __index does and i dont exactly get why people do stuff like

local module = {}
module.__index = module

return module

Anyways besides that, i dont see a use currently on why they are being used, so im just wondering in what situations should they be used and why

Require that module and attempt to index any key, see what happens.

1 Like

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?

1 Like

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

(i thought it would print 2 but it didnt)

wdym by any key? just something random or

__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:

4 Likes

Ill definitely check it out :+1:

local t = {}
setmetatable(t, {__index = t})
print(t.k)
1 Like

Tried pasting it in and got
Workspace.Script:10: loop in gettable
this is spose to just be in a normal script right?

It’s supposed to cause an exception, which is what you encountered.

so is that bad or good?
Not sure what im spose to be getting from this

You maybe forgot local _index = module

local module = {}
local _index = module
module.__index = module -- you can be rid of that.

return module

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.

Is this a better method to use instead of .__index?

Oh, ok so how would i advoid doing this? do i just not set normal tables’s metatables to themselves and just do it for modules or?

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()

ok, i get what you are saying, but just to be careful i can a modules metatable to itself right?

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:

@simplyjustbased

It should do that. The reason your code didn’t do that was because you capitalized one of the functions, which meant they were different.

oh, i just saw that thanks for pointing it out