Maintaining a Singleton design Pattern

How would I setup my ModuleScript and interact with it to maintain a Singleton design (a single instance)?
I’ve been running into cyclic table issues since I’m just referencing the ModuleScript itself when trying to refer to it through bindables or a remote.

Do I just not use setmetatable to set it?

My general classes are setup as so:

local ExampleRepo = {}
ExampleRepo.__index = ExampleRepo 
ExampleRepo.ClassName = "ExampleRepo"

function ExampleRepo:init()
    setmetatable(self, ExampleRepo)
    return self
end

return ExampleRepo
2 Likes

No need for a metatable if you’ve just got the one table :slight_smile:

local MySingleton = {
    SomeProperty = "Hello"
}

function MySingleton:DoSomething()
    print(MySingleton.SomeProperty)
end

return MySingleton
1 Like

Good point I guess I was going in the right direction, just was confused with metatables I suppose.

Just FYI you can still do self.SomeProperty also if you want