I think you’ll also have to return self so it’s set to the function. Or set self to a meta table and make a variable called A and set it to 2. and make a method to print it.
Oh wait, When you run it through the console, you first have to run the function, like in a normal script. Otherwise it’s going to return nil because it can’t find any value in the function.
@JAcoboiskaka1121 Thanks, I got a better explanation from another guy from Discord, who said it’s because requiring the module script in the console re-runs the module.
For example, if you have
local module = {}
print("ran")
function module:F()
self["A"] = 2
print(self)
end
return module
Running require(game.ReplicatedStorage.ModuleScript):F() in a script will print
ran
{ ["A"] = 2, ["F"] = function }
Running print(require(game.ReplicatedStorage.ModuleScript)["A"]) in another script will print “2” as expected.
However, running print(require(game.ReplicatedStorage.ModuleScript)["A"]) in the console is like running a script in a different actor, resetting the module to { ["F"] = function }. You can see it prints ran again upon requiring the module in the console for the first time. Therefore, you need to re-call module:F() in the console for it to register.
TL;DR: running code in the console is like running a script in a different actor, which resets the module.
Did you seriously not read what I just wrote above, let alone try it. The code actually works because I tested it. This is basically what I said except complicated. Modules aren’t cached; they do not come from the same environment as the module script instance itself. When you require it, you basically create a separate environment.
At least acknowledge what people write to help you because they took time themselves to figure out a solution for you.
The reason why the behavior happens is that studio command line is actually a separate environment from the server, therefore not sharing the same cached value.
That is literally what I said, it’s only from the same environment. Here he is using Object Orientated Programming, and is where you need to declare it as a variable.