Are tables better than individual variables?

I’ve only recently learned about this.

Apparently you could use tables to organize variables, like for example:

[INDIVIDUAL VARIABLES]

local intermissionStarted =  getServices.replicatedStorage.Events.IntermissionStarted,
local roundStarted =  getServices.replicatedStorage.Events.RoundStarted

vs.
[TABLES]

local getEvents = {
	intermissionStarted =  getServices.replicatedStorage.Events.IntermissionStarted,
	roundStarted =  getServices.replicatedStorage.Events.RoundStarted
}

Now my question is, are tables better than individual variables. If so, how? Are they faster in any way, or are they just for organization?

No and Yes.
Here are a few reasons

Advantages

  • Can store Multiple Things
  • Is used is many things

Disadvantages

  • Is Lengthy
  • Problems with finding things in table
  • Can sometimes cause errors

So I guess in a way they are and in a way they aren’t. Now its all up to you to either use them or not!

P.S! (Sorry if I missed something in advantages!)

1 Like

They way your using them isn’t the best way tbh I consider using individual variables.

What if it’s multiple variables for example, should I use tables?

Yes then you should for example

:x:

local Part
local Part2
local Part3

:white_check_mark:

local Parts = {Part,Part2,Part3}

You can see how less lines it uses

Note: Tables are groups in scripting

1 Like

I am actually rn legit suffering with tables lol prob cuz of my code

Yeah, I would recommend tables for lots of variables that can go under the same description E.g. local Scores = {95, 50, 25, 10} or a function that takes lots of parameters, something like tween instructions.

Hope this helps!

1 Like

For those two RemoteEvent instances it would be better to just leave them as variable declarations instead of inserting them into a dictionary, otherwise you’ll need to index this dictionary with the necessary key each time you intend to reference either RemoteEvent instance as opposed to referencing them by their variable names of which they are assigned to.

1 Like

The “finding things in table” is really only a problem with array tables, you can use a dictionary table and it have the same beneifits as an variable

local Dict = {
    A = workspace.Part,
}

print(Dict.A) -- Part

Don’t know what you mean by sometimes causing errors, but if a table is causing you an error then its most likely your fault, very few things on roblox just “randomly” error without an explaination

1 Like

I agree, I think dictionaries are great mainly for organised code, and like you said, shouldn’t cause any errors given you use them correctly.

It’s down to preference really, some people like:

local thing1 = this
local thing2 = that

Or:

local thing1, thing2 = this, that

Or:

local things = {
    thing1 = this,
    thing2 = that
}

I mainly use the second version when I have similar/related variables.