Question about variables in ModuleScripts

Quick question, is it bad practice to have variables in a module script that are not part of functions that can be required?

For example:

local module = {}

local itemFolder = workspace.ItemFolder -- I'm talking about this line right here

module.MoveItem = function(item)
item.Parent = itemFolder
end

return module

Also, in the example below, which way of creating a function is “better” or is it just personal preference?

local module = {}

module.funcA = function()
print("Option A")
end

function module.funcB()
print("Option B")
end

return module

Thanks :slight_smile:

not at all, your functions are making use of them.

the bottom one is syntactic sugar (a nicer way to write something) for the top one. I recommend always using one unless a specific circumstance says otherwise. consistency is important when writing clean and maintainable code.

so yes it is preference.

also, it’s not like you can’t have functions in tables without modules, you could do this in another type of script as well.

1 Like

i personally don’t think using variables like local itemFolder = workspace.ItemFolder in a module script is a bad practice at all, why would it be. The nice thing about them is, well, their local, they wont get returned when the module is required or at least when they are not specifically returned(because they are not apart of the table that is returned). i would just use then when you should or when it’s more practical, often times like when you need to define properties of things/objects that’s where you might not want to do that (for me this is more to do with my OOP habits).

They do the exact same thing, personally its cleaner for me to use module.funcB() but that’s really just a personal preference

1 Like

Alrighty, I figured it was probably fine to use variables in a ModuleScript and also that the second version of creating module functions was cleaner but since I’m mostly self-taught I wanted some outside input. I think my main use for variables like I described would be for easy reference of objects that won’t change and they’re more there as quick references to help keep code clean so I don’t expect I’d need a lot of them.

Just so this isn’t misleading, even if the variable is global (which it shouldn’t be), it won’t be picked up by the require call. It’s what is returned that counts.

1 Like

Good catch! i was more or so trying to say using local as in local Part = script.parent variables themselves wont be returned, unless you specifically return them.