Is it possible to make __index Fire when the table[index] has a value

I have been trying to figure out if there is a way to make sure __index gets called when the value is inside table

local MyMetaTable = setmetatable({Hey = "Hello"}, {
	__index = function(self, Index)
		print(Index)
	end,
})

print(MyMetaTable.e) -- print MyMetaTable[index]
print(MyMetaTable.Hey) -- doesn't fire __index

No, in lua (and luau) __index will only be used when table[index] is nil. There’s no way to change this unless you actually add it into luau. However, what you can do is something else:

local MyMetaTable = setmetatable({__meta={}}, {
    __index = function(tbl, index)
        print("Index fired")
        if tbl.__meta[index] then return tbl.__meta[index] end
    end,
    __newindex = function(tbl, index, value)
        if tbl.__meta[index] == nil then tbl.__meta[index] = value end
    end
})

print(MyMetaTable.e) -- prints "Index fired" and nil
MyMetaTable.Hey = "Hello"
print(MyMetaTable.Hey) -- prints "Index fired" and "Hello"

This essentially keeps all keys in a table within the table which means MyMetaTable[index] other than __meta is always nil which means __index will always be fired.

However, with this particular setup you can only set values which are currently nil, but you can change that functionality simply:

__newindex = function(tbl, index, value)
    tbl.__meta[index] = value
end

Basically this just means it will always override anything, if that’s what you prefer (which is the normal table[index] functionality)