How to call metafunction by variable name?

Hello guys. I’m making tools module scripts which use metatables in order to store some important tool data. But I have got need to call this metafunctions with variables, like this:

local MetaModule = {}
MetaModule.__index = MetaModule

function MetaModule:MouseBtn1(Variable)
    self.Var = Variable + 1
    --do smth
end

function MetaModule:MouseBtn2(Variable)
    self.Var = Variable - 1
    --do smth
end

return setmetatable({}, MetaModule)

And other script which calling it like this:

local Module = require(ModuleAbove)
local FuncName = "MouseBtn1"
local Variable = 12
Module[FuncName](Variable)

Example above won’t parse “self” to function ModuleMeta:Test(). How I can make it so self will be parsed?

Whenever executing module[funcName](params), it does not infer the self into it and you’ll always have to do module[funcName](module, params). Otherwise, it’s just module:funcName(params)(colons are the only sugar syntax).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.