Is it possible to use self globally across scripts?

How can you use self across scripts (if it’s possible)? I want to be able to do it like this because I’ve seen other people use self like this but I’ve had no luck.

local module = {}
local RunService = game:GetService("RunService")

function module:Start()
       self.IsStudio = RunService:IsStudio()
end

return module```

Different script:

local module = {}

function module:Load()
wait(1.25)
local isStudio = self.IsStudio
end

return module```

Is this possible in any way?

1 Like

If it’s only 2 scripts, you can assign the “Different script” module’s index to the module with the method “Start”:

local module = {}

function module:Load()
    wait(1.25)
    local isStudio = self.IsStudio
end

return setmetatable(
    module,
    {
        __index = require() -- Path to other module
    }
)

This is assuming your other scripts doesn’t use methods but just assigns variables.
So, the other script should look like this:

local RunService = game:GetService("RunService")

local module = {}
function module:Start() -- Remove this method
       self.IsStudio = RunService:IsStudio()
end
module.IsStudio = RunService:IsStudio() -- Replace it with this part
return module

I wouldn’t recommend doing this though. Try and see if you can combine the two modules together. Alternatively, you can create a third module in which both modules would require the third module to get the variable data.

1 Like

Are you sure you’ve seen “self being globally used” or are you confusing that with uses of either setfenv, metatables or a missing require? Self in both cases is going to point to the module table no matter what because the table is implicitly passed by right of you calling the methods with a colon.

Regardless of what it is, this is bad practice and it’d look horrible on your code’s readability. If your use case is specifically IsStudio then you don’t need a module dedicated for that, just call it directly for RunService. If it’s just an example then don’t follow in the footsteps of whatever you saw.

Proper code will rarely ever need to use self. You will see that more commonly in frameworks or OOP so methods can be called at a service-level versus an object-level. Other cases of self are just weird, unnecessary or a preference of the developer (whether for better or for worse).

1 Like

I can link you and @VegetationBush to the video I saw on this. Looks like this guy is developing something for a group and he is scripting on stream and his code was very interesting to look at. I’m probably defining everything he is doing incorrectly, see if you guys can explain it to me. - YouTube

This looks like OOP. The developer in question is making a Vehicle class, so self is the Vehicle table which the methods are being added to. Modules then, going by the flow of the code, is a table of other module references that the developer can call from in a Vehicle method to access other modules.

This is not using self globally. Simple example:

One module:

local RunService = game:GetService("RunService")

local module = {}

function module:Get()
    return self.IsStudio
end

function module:Start()
    self.IsStudio = RunService:IsStudio()
end

return module

Another module:

local module = {modules = {}}

function module:Print()
    print(self.modules.IsStudio:Get())
end

function module:Init()
    self.modules.IsStudio = require(is_studio_module_here)
end

return module

And a script:

local foobar_a = require(first_module_here)
local foobar_b = require(second_module_here)

foobar_a:Start()
foobar_b:Init()

foobar_b:Print()
2 Likes

Ah I see, thanks for explaining it to me. I knew I wasn’t explaining it correctly.