Storing most frequently used variables in module script

Wouldn’t it be better for performance if I would make module script that store the most frequently used variables and doesn’t repeat myself in the scripts?
Example:

--Module script
local module = {
	starterGui = game:GetService("StarterGui")
}

return module
--Script/local script
module = require(module location)
--Example code
liblary.starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

insted of this

--Script/local script
local starterGui = game:GetService("StarterGui")
--Example code
starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

I don’t know how to check if it would give some performance, what do you think about this, guys?

  • use module scripts to store variables
  • use local variables

0 voters

2 Likes

I don’t know if this is in the right category, but I only use modules for variables if it’s an object that changes. For example, a gun’s weapon settings will be a module.

Yes, it is in the right category I think “The Code Review category is intended to be a place where developers can get tips to improve already-working code they have created for Roblox games, or provide feedback on code of others.” I’m showing code that is working and want to know if this is better to use

--Module script
local module = {
	starterGui = game:GetService("StarterGui")
}

return module
--Script/local script
module = require(module location)
--Example code
liblary.starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

instead of this

--Script/local script
local starterGui = game:GetService("StarterGui")
--Example code
starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

and you didn’t really answer my question, but thanks for trying to help.

A mix;

For modules:
Values that are dynamic and used by multiple scripts (such as config) I would recommend using a module script so you have a one-stop place to change those variables. Additionally, I would also recommend putting functions used by multiple scripts in modules scripts.

For scripts:
Essentially everything else, this includes services, local functions, and whatever else you can think of. The reason why you would likely want to store services in the script is that they are static, and is just more common practice.


The question you actually asked

Saving variables without using a module script is always going to be more performant except in rare edge cases where the values take a lot of time to compute. The real thing though is that performance doesn’t usually matter since lua is an insanely fast language allready and I would value organization over performance in most cases.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.