Hey, I have tool class named functions in a metatable, and I have a default function, which I’ll fire if the tool class name isn’t in the metatable, which would be best for such case?
if (not ToolClassFunction) then return Metatable:DefaultFunction() end;
or
return Metatable[ToolClassName](Metatable, ...);
How I’ve set up the metatable:
local Metatable = {};
Metatable.__index = function(self, ...)
print('Default');
return
end
Btw I’ve forgot how to do __index
functions
so it prints, but still errors (attempt to call a nil value), unless I do:
__index = function() return function() print('xd') end end
which I don’t know if I should do lol.
Edit: Alright I can’t wait
, I’ll just go with the former, so I can set __index to self, if anyone says the latter is better I’ll change it lol.
1 Like
I’d personally probably just have the __index
metamethod reference the table with your function definitions (I think you’re already doing that.) But if you’re curious, here’s how you could get the alternative working.
local Metatable = {}
function Metatable.someTool()
print'yeaaah'
end
function Metatable.default()
print'default yessir'
end
setmetatable(Metatable, {
__index = function(t, k)
local memberFunction = rawget(Metatable, k)
if memberFunction then
return memberFunction
else
return Metatable.default
end
end;
})
Metatable.thisDoesntExist() --> default function gets called
1 Like
Well I don’t know if rawget()
would work with ‘:’ functions (table[string](table, ...)
), but I ended up just using the if statement, thanks though 
Edit: For anyone curious to what I was doing, here the code:
local ClassNameFunction = ClientInstanceService[ToolClassInstance:GetAttribute('ClassName')];
return not ClassNameFunction and ClientInstanceService:DefaultActiavteInstance(ToolClassInstance) or ClassNameFunction(ClientInstanceService, ToolClassInstance);
1 Like