Variables in Module script

Hi I’m just confused about module script. I’ve learnt it from this website Intro To Module Scripts (roblox.com)

However, I’m just wondering what’s the difference if we put local variable after the module table and if we put local variable before the module table?

3 Likes

I suggest watching AlvinBlox on youtube for module scripts for this. He explains it well.

I’m learning to use Modules too, what u mean by put a local variable after or before the “table”? I checked that tutorial, but I dont understand what u mean? any example?

EDIT:
maybe you mean this?

local somethingBefore = "emm"

local module = {}
-- module functions

return module

local somethingAfter = "emm..."

idk what does that… You could use that maybe, for doing some functions inside that “module” script and other scripts are unable to use that vars and functions… but, thats not the idea of using a module, that “table” is the module itself, in order to be accessed from other scripts all vars and functions should be declared inside the “table” /module. Using the module name.

What are you trying to say? I’m very lightheaded from understanding this.

There won’t be a difference in where you place the local variables.

A Module Script has to return something, it doesn’t however have to return everything. In most cases it is the Module Script table that is returned.

local ModuleName = {}
return ModuleName

Anything added to that table will be returned by the require in other scripts.

local ModuleName = {}
ModuleName.SomeVariable = "Some Value"
ModuleName.SomeTable = {}
return ModuleName

So in some other script somewhere,

local ModuleName = require(PathTo.ModuleName)

At this point, you can access whatever is returned from the Module Script, which consequentially is pointed to by the variable above.

local ModuleName = require(PathTo.ModuleName)
print(ModuleName.SomeVariable) -- prints "Some Value"

Because local variables are not included directly in the table, they don’t get the same privilege. They are considered local to the Module Script itself. However, there are ways to “set” or “get” those local variables from the outside source requiring it.

local ThisVariableIsAbove = true
local ModuleName = {}
local VariableNotInModuleNameTable = "This is some text"

function ModuleName.SetText(text)
VariableNotInModuleNameTable = text
end

function ModuleName.GetText(text)
return VariableNotInModuleNameTable
end

function ModuleName.IsVariableAbove()
return ThisVariableIsAbove
end

return ModuleName

Now that you have included these two functions as part of the Module Scripts table, you can do the following in another script.

local ModuleName = require(PathTo.ModuleName)
print (ModuleName.GetText()) -- prints This is some text
ModuleName.SetText("This is some set text")
print(ModuleName.GetText()) -- prints This is some set text
print(ModuleName.IsVariableAbove()) -- prints true

Hopefully that helps a bit with understanding the basic powers of Module Scripting with variables.

14 Likes

I don’t think it makes a difference at all.

Yea the module table I meant is the

local module = {}