How to make a class In OOP

I wanna put a Function inside a table or another OOP rather inside OOP

to put this simply, yk how you can do this

local table = {
    hi = {}
}

function table.hi.Hello!()

end

and ofc when I call that its gonna be

table.hit.Hello()

basically I wanna do it in OOP

To put this in another perspective, which is the perspective on calling it;

local OOPMethod = require(the.table)

local OOP = OOPMethod.New()

OOP.AnotherTable:AFunction()

I just dont get the logic on how can I do that, Please help me

1 Like

If you want to do this with the constructor, you can do something like this:

local class = {}
class.__index = class

function class.new(test : number)
	local self = setmetatable({}, class)
	self.test = test
	
	return test
end

return { hit = class } -- Like this, so now, you have to type out module.hit.new.

if you want to make it for methods, I think you can just make 2 classes, an inner and outer one, so when the outer class gets called, it makes a new inner class object with all the methods, though probably not the best solution probably.

like this:

local innerClass = {}
innerClass.__index = innerClass

local outerClass = {}
outerClass.__index = outerClass

function innerClass.new()
	local self = setmetatable({}, innerClass)
	return self
end

function innerClass:test()
	print("The test function is working")
end

function outerClass.new()
	local self = setmetatable({}, outerClass)
	self.tbl = innerClass.new()
	
	return self
end

return outerClass

here is how you can use it:

local mod = require("@self/ModuleScript") -- module location

local a = mod.new()
print(a.tbl:test())
1 Like

thanks! Im satisfied with this solution

2 Likes

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