I’m working on an OOP that allows me to access Functions from another Module - However, my issue is if I were to have the function inside another table: i.e.
self.Function = { }
self.OtherTable = {}
self.Function:Test()
function Module:Test()
end
function Module:OtherTest()
end
It’ll print the self for the ‘Module’ table inside another Script but will not print the required Self that it’s a part of? Does anyone have any suggestions?
I’ve never seen anyone structure Lua OOP like this. What even is the self table in the main scope and what does that have to do with Module? You need to show us the entire script because it’s missing context
The typical approach to OOP in Lua is to use metatables. The following code should go in a ModuleScript:
local MyClass = {}
MyClass.__index = MyClass
function MyClass.new()
return setmetatable({}, MyClass)
end
function MyClass:Test()
print("Test was called")
print("self:", self)
end
From another script you can require the ModuleScript:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MyClass = require(ReplicatedStorage.MyClass) --Change path to wherever you keep the MyClass ModuleScript
local class1 = MyClass.new()
class1:Test()