How do I use services in a oop class instance

Whats the best way to store servieces in oop instance?
I cant really explain it in words, so I’ll use an example

local myClass = {}
myClass.__index = myClass

function myClass:CheckTag(obj)
   -- MY CODE
end

function myClass.new()
   return setmetatable({}, myClass)
end

say I have a class like one above. The class written in a module script thus no access to variables outside the module script (at least, not way to wirte it in the module scirpt with \out passing them down though some function)
In the “-- MY CODE” I want to use some roblox service.
Normally if it inst a class I’d just write something like

local service = game:GetService("SomeService")

however, the class instance would’t have accesss to it as that would be local to the module script. So what I did is

myClass.someService = game:GetService("SomeService")

it works but the problem I’m feeling is that this way, every single instance would have the service stored, even tough that service is a static value and has no relation to the actual instance itself.
Yes I realize this doen’t affect performance at all considering its only a small amout to data, but the code is storing unnesseary data. Is there a better way to access services?

Not really sure what you mean by this. If you define “service” inside the module script and then create a new object with myClass.new(), the object should have access to the service you defined in the module script

local service = game:GetService("Players")

local myClass = {}
myClass.__index = myClass

function myClass:CheckTag(obj)
	print(service, "service")
end

function myClass.new()
	return setmetatable({}, myClass)
end

return myClass

Another script:

local myClass = require(script.myClass)

local obj = myClass.new()

obj:CheckTag() --> Players service
1 Like

In lua we implement OOP with normal tables and functions without affecting the scoping rules. The scope in lua is lexical, so you just have to make sure that a variable is in the scope of your function. If you put "local service = game:GetService(“SomeService”)
"at the beginning of your module this service will be in scope of your “myClass:CheckTag()” method regardless of the instance that calls it.

On the other hand “game:GetService(“SomeService”)” returns the reference to the instance (singleton) of a service. if the instance does not exist it will create it only the first time. meaning that, in the worst case only the first call to your “myClass:CheckTag()” method will be affected by the overhead of creating the service, the rest will simply handle the reference directly.

2 Likes

If you put "local service = game:GetService(“SomeService”)
"at the beginning of your module this service will be in scope of your “myClass:CheckTag()” method regardless of the instance that calls it.

I did not know about that at all! I though sice the instance’s scope would be at the script it was retunred to
I guess I need to relearn lua scopes…

One more question.
If I understand correctly if I change a variables thats in the scope of the module script, would all the instances get an updated value? or is the variable only copied at the time of its instance’s creation?

Yes, you will get the updated value. Any instance, no matter where or when it was created, when calling one of its methods, the context will change to the module where the method is defined, then the method will use the current value of the variables in its scope.

In lua references are used a lot, so copies are not usually created (for efficiency reasons).

1 Like

Overhead shouldn’t come into play because the module will compile and execute itself when required, which means that any variable declarations assigned some service object (retrieved by the instance method :GetService()" should exist from the get-go and thus be referable immediately (within the scope they are declared in).