Is this a good way to categorize my variables?

Lately I’ve been finding my main script has been using a lot of variables and local functions. My game has one big client local script to handle nearly everything AND also a lot of module scripts to perform different jobs.

Allow me to screenshot code such that it will be more easily to visualize them.
This is what it looks like BEFORE I categorize them:
image
image

AFTER SOME categorization

image
image

Overall, is this better? I pretty much utilized tables to store things rather than creating a new variable. I’m not quite sure their difference in terms of performance but I think they should be similar? At least by doing this I won’t run into the “out of local registers” error.

we all prefer our own ways of storing variables some may not be as optimal in terms of performance but because you asked, it technically is having to look through a table to get a returned value meaning it is doing extra work, but the difference won’t be that noticeable until you index for example ContentProvider 10k times and actually do a benchmark on it

Looks kinda clean, however, this choice is always depending on yourself. One rule on coding is that code for readability, NOT to make it look cool and unecessary. And this depend on you to personality, if you think short words are good readability for you, go for it.

1 Like

I’ve sometimes made by variables into arrays or tables as they seem a bit more organised;

local UIs = {
player.PlayerGui.Score, 
player.PlayerGui.Kills
}
print(UIs[1].TextLabel.Text)

also module scripts are good when you’re using a bunch of scripts with the same variables

Does “index 10k time” mean I call Services.ContentProvider for 10k times?

Hey I have a trick I have been using forever to get services, this allows you to not have to type :GetService() and “” all the time for each service:


local S = setmetatable({}, {__index = function(_, s) return game:GetService(s) end})

local plr = S.Players.LocalPlayer

local serverScriptService = S.ServerScriptService

This is a huge shortcut :sunglasses:

1 Like

ModuleScripts are your friend! You can break down functionality into separate scripts, which helps you stay more organised, in addition to making code reusable and easier to maintain.