How to prevent repeating of same variables?


I have two seperate module scripts, one that deals with the time, one that deals with the status. Problem I’ve just realised is both use the same set of variables (the ones getting the stuff inside the GUI)

But if I was to move around parts in the GUI, change names, etc. I’d have to go back into both and fiddle around the variables and what not to get them right, and worse if I have more scripts that all reference the same GUI. Is there a way to store like the variables inside a script or module or table or something, and have that passed around to all the modules? so if I change anything in the gui I can just change it in one script and all the other scripts will just look off the 'variables script?

Cant you just make another module??

return {
var = val....
}
local Variables = require(commonvars)
print(Variables.var)

or if for some reason youre super lazy and like global variables

local _ENV = getfenv()
for k,v in next,require(commonvars) do
    _ENV[k] = v --NOT RECOMMENDED, this is what Luas old 'module()' could do
    --Problems with conflicting global names and also less readable and typically
    --shown as an undefined variable with the syntax highlighter
    --Also harder to debug name changes and other issues
end
print(var)

I’d design it so that I can pass in the signal to listen to and the text label to output to. TimerManager would then look like:

local TimerManager = {}

function TimerManager:connectTimeChanged(signal, textLabel)
	signal:Connect(function(newValue)
		-- Code here
		textLabel.Text = ...
	end)
end

return TimerManager

And then in your main script, do something like:

-- Get replicated storage etc.
local updateTimer = events:WaitForChild("UpdateTimer")

-- Get the gui objects etc.
local timer = timerBar:WaitForChild("Timer")

-- Set up the timer manager
local TimerManager = require(Modules.TimerManager)
TimerManager:connectTimeChanged(UpdateTimer.OnClientEvent, timer)

The benefit you get from doing it this way, is it’s now testable:

-- Simple implementation of a custom signal
local Signal = {}
Signal.__index = Signal

function Signal.new()
	return setmetatable({
		connections = {}
	}, Signal)
end

function Signal:connect(func)
	self.connections[func] = true
end

function Signal:fire(...)
	for func, _ in ipairs(self.connections) do
		func(...)
	end
end

local function testTimeChanged()
	local TimerManager = require(Modules.TimerManager)
	local testLabel = Instance.new("TextLabel")

	local signal = Signal.new()

	TimerManager:connectTimeChanged(signal, testLabel)

	signal:fire(fakeData)

	assert(testLabel.Text == "some example")
    testLabel:Destroy()
end
3 Likes

I managed to figure out a way to do it XD Not sure if this is the ‘best way’ to go about it, but it works so :man_shrugging: who am I to complain right XD

 local variable = {}

variable.Client = script.Parent

variable.HUD = variable.Client.HUD
variable.GameHUD = variable.HUD.GameHUD
variable.LobbyHUD = variable.HUD.LobbyHUD

variable.Deploy = variable.LobbyHUD.Deploy
variable.Status = variable.LobbyHUD.Status

variable.ScoreXY = variable.GameHUD.ScoreXY
variable.ScoreYY = variable.ScoreXY.ScoreYY
variable.Gamemode = variable.ScoreYY.Gamemode
variable.TimerBar = variable.ScoreYY.TimerBar
variable.Timer = variable.TimerBar.Timer

return variable

And so all I need to do is call the module, then just go say variable.Timer.Text = time or whatever and yea :smiley:

Hope this helps someone in the future :smiley:

3 Likes

already suggested that

6 Likes