Is there a way to find "how" a metamethod (__index) was fired and can I get the arguments passed if there were any?

I just want to do this more for cosmetic purposes but is there a way to get “how” the metamethod was fired? For example, let’s say I have the following

local replicatedStorage = game:GetService('ReplicatedStorage')
local myModule = require(replicatedStorage.MyModule)

local var1 = myModule.var1
local var2 = myModule.var2()
local var3 = myModule:var3() -- or myModule.asd(myModule)

Now in the module, let’s say all of these don’t exist. Is there a way to discern how it was called?

var1 would be variable or index table
var2 would be function

And for the second part of the question,
var3 would be function again but you’d have to check the first passed variable in the function which is why I would want to be able to get the arguments passed.

1 Like

Nope, no metamethod for that, the only thing SIMILAR is __call which you might be able to build something like that. But when you index a function, you’re returning that function, to THEN execute it, so it’s pretty much impossible for that to exist.

2 Likes

There are hacky ways to do it, but it’s kind of useless. I don’t really see a point in using this

local myModule = {}

function myModule:test()
    print('testing')
end

for i, v in pairs(myModule) do
    if type(v) == "function" then
        myModule[i] = setmetatable({}, {
            __call = function(_, t, ...)
                print(t, ...) -- (table, ...)
                return v(...)
            end
        })
    end
end

myModule:test("foo", "bar")
1 Like