Found your problem. BaseSword class is useless in this scenario because you arenât taking any of the methods from BaseSword. Handler doesnât take from BaseSword, but vice versa is applicable.
When youâre creating a class that inherits from another, you donât make a new object of the superclass. The methods are automatically pulled in because your class table has the superclass table as a metamethod. Make a new object from the class itself.
Proper inheritance needs to follow this workflow: you can replicate it to the best of your ability with what you have now. Here is what your superclass should look like, which it seems you already got that part down.
local Tool = {}
Tool.__index = Tool
function Tool.new()
return setmetatable({}, Tool)
end
function Tool:PrimaryAction()
print("Tool PrimaryAction")
end
Now for your BaseSword subclass, this is what youâre currently doing.
-- Take methods from Tool class as meta
local BaseSword = setmetatable({}, Tool)
-- Fall back to BaseSword if the object doesn't have the queried
-- index, or to BaseSword's metatable if an index
-- can't be found in the table either.
BaseSword.__index = BaseSword
function BaseSword.new()
-- BaseSword is useless here, because you are
-- not making a BaseSword object. The class
-- becomes completely useless.
return Tool.new()
end
function BaseSword:PrimaryAction()
print("BaseSword PrimaryAction")
end)
Test it like this and Tool PrimaryAction will fire instead. You can fix this simply by changing your constructor function to make an object of the BaseSword class.
function BaseSword.new()
return setmetatable({}, BaseSword)
end
Now BaseSword PrimaryAction will fire.
Why does this happen? The __index of your tool object becomes BaseSword, which due to inheritance has an __index of Tool. Therefore: if the index you query doesnât exist in the Tool object, it looks in the meta index which is BaseSword. If it doesnât exist in BaseSword, it looks at that meta index which is Tool.
Tried to explain it as simply as I could. Do you follow?
tl;dr your BaseSword constructor is currently just a wrapper for directly doing Tool.new. Tool does not have any BaseSword methods, only Tool methods. That is why Toolâs PrimaryAction fires but BaseSwordâs does not.