"Attempt to call missing method of table" in an inherited class

Howdy!

I’m trying to make a new class which is inherited from a base class, where the new class will be able to hold methods independent of the base class. However, whenever I try to call a method from the non-inherited class that isn’t from the inherited class, I get the error stated in the topic name.

This is not my code in the project, but it goes something like this

local ToBeInherited = {}
ToBeInherited.__index = ToBeInherited

function ToBeInherited.new()
    local self = setmetatable({}, ToBeInherited)

    return self
end

function ToBeInherited:DoAThing()
    print("Doing A Thing")
end

-- ^ MODULESCRIPT A ^ --
----------------------------------------------------------------
-- V MODULESCRIPT B V --

local Class = setmetatable({}, ToBeInherited)
Class.__index = Class

function Class.new()
    local Base = ToBeInherited.new()
    local self = setmetatable(Base, Class)

    return self
end

function Class:DoAnotherThing()
    print("Doing Another Thing")
end

----------------------------------------------------------------
-- V INSTANTIATING THE CLASSES V --

local newClass = Class.new()
newClass:DoAThing()
newClass:DoAnotherThing()

newClass:DoAThing() will work as intended, but newClass:DoAnotherThing() will return an error, it’s like the methods in the non-inherited class are being ignored entirely. Is this a drawback to roblox oop? Is there a workaround or am i doing something wrong?

Thank you for your time ^.^

5 Likes

In your example, your [Class] is inheriting the methods of [ToBeInherited]

As such your example code does work

local newClass = Class.new()
newClass:DoAThing()
newClass:DoAnotherThing()

The most likely answer is you switched around your base class and your new class

--Car inherits traits from vehicle
local Car = setmetatable({}, Vehicle)

Another answer could be you forgot to give the base class the called method

function Vehicle.new()
    --new vehicle
end

function Vehicle:Move()
    --idk moves
end

--Car inherits traits from vehicle
local Car = setmetatable({}, Vehicle)
Car:Fly() --“Attempt to call missing method of table”
2 Likes

Heyy I managed to get it working now, though I don’t know how if I were honest lol. The example script made above was made by eyeing back and forth between the actual script and the example. I ended up brute forcing the script the example script and substituting the variable names after you said it was the example was correct, which after 2 tries magically seemed to work.

Thanks for the help! I’ll mark it as the solution as pointing the example out as correct led me to fixing my problem!! :smiley:

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