Using variables to improve readability of code: Is it worth it?

Hi all,

I’m a new scripter and I was working on a script for a simulator type game for practice. I created a script for the ore/rocks, but it’s kind of messy (Here’s an example):


Q:Is is possible to reference an index of a table instead of the value?

First Question

The above table reference (and others ones) crowd out my code. I could put the value into a variable:

local CoolDown = Table[OtherTable[IndexOne]][2]

This is much easier to understand, especially if it’s used multiple times, but let’s say I want to change the value of the CoolDown (The table value not the variable)

local CoolDown = Table[OtherTable[IndexOne]][2]

CoolDown = 55

This won’t change the actual table, only the variable CoolDown.

My question is: Is there any way I can change the value in the table without having to type:

Table[OtherTable[IndexOne]][2] = 55

Q:Is it worth using variable if I only use the reference once?

Second question
Script Example

Here’s a function/coroutine that resets a mined rock after it’s cooldown is complete:

local RockCoolDownCoroutine = coroutine.wrap (function()	
	while true do	
		for NumberOfRocks = 1, 4, 1 do	
			if RockCoolDownTable[ItemTable["Rock"][NumberOfRocks]] > 0 then
				RockCoolDownTable[ItemTable["Rock"][NumberOfRocks]] = RockCoolDownTable[ItemTable["Rock"][NumberOfRocks]] - 1

				if RockCoolDownTable[ItemTable["Rock"][NumberOfRocks]] == 0 then
					game.Workspace[ItemTable["Rock"][NumberOfRocks]].BrickColor =[ItemTable["Rock"][NumberOfRocks]][3]) 
					CurrentRockStrengthTable[ItemTable["Rock"][NumberOfRocks]] = OriginalStrength = RockDataTable[ItemTable["Rock"][NumberOfRocks]]
			RockCoolDownTable[ItemTable["Rock"][NumberOfRocks]] = CoolDown


My other question is: Is it worth putting these references into variables?

I mean, It makes sense if I use the reference a lot, but sometimes I only use it once or twice, is it really worth adding another line of code to my script to make one or two other ones more readable?

This is kind of a nitpick, but I want to get this right the first time. I know an extra variable won’t make a dent on my memory usage (I heard that this is what you want to measure performance wise (correct me if I’m wrong)) but is it counter productive when it comes to cleaning up my code?

Here’s an example:

Rock.BrickColor =[ItemTable["Rock"][NumberOfRocks]][3]) 
local OriginalColor = RockDataTable[ItemTable["Rock"][NumberOfRocks]][3]
Rock.BrickColor = 

I only use this variable once. It makes the second line so much easier to understand, but is it worth a whole other line of code?

Thank you for any feedback! (any feedback on the coroutine in general is also welcome).

1 Like

Variables are an important part of scripting, I would suggest using them especially if take a break and you return to a project later down the line. Otherwise you will be majorly confused the specifics of a table. I would also suggest just putting down a comment of how the table is ordered

-- Rock = {"Name", "Cooldown", "Color", etc...}