How to do inheritance in luau OOP

This is for people who already know what oop and inheritance are but just dont know how to implement it in luau due to luau lacking class types and us having to do weird things to make oop work.

local SuperClass = {}
SuperClass.__index = SuperClass

function SuperClass.new()
end

local SubClass = {}
SubClass.__index = setmetatable(SubClass, SuperClass)

function SubClass.new()
    local Object = setmetatable(SuperClass.new(), SubClass)
   return Object
end

EDIT: Removed previous example as people where getting confused with the complexity of it.

4 Likes

I recommend putting this post inside community resources since there isn’t any steps explaining how to script aging. Otherwise, great job :+1:

Hey, this is already in community resource im pretty sure and the tutorial is not about the object within the class but rather about how you can allow a sub class to inherit from a super class.

Here the human class inherits from the Entity class. There were no easily accessible tutorials of this that I could find so I decided to make one.

TL;DR:

local SuperClass = {}
SuperClass.__index = SuperClass

function SuperClass.new()
end

local SubClass = {}
SubClass.__index = setmetatable(SubClass, SuperClass)

function SubClass.new()
    local Object = setmetatable(SuperClass.new(), SubClass)
   return Object
end

Thanks, i didn’t know Age was a number all along

1 Like