Med Written By nick_namous

Tables Created on: 24-11-2019

Intro to variadic tables & their uses.

Table Intro

Authored by: nick_namous

Tables allow for an interactive game, intricate scripts, and fun; they are a necessity for any Roblox game to prosper.

Variables

To know and understand how to make tables, you must be able to successfully create and understand how a variable works.

Variables are one of the fundamental building blocks of script. It enables scripts to be less complicated, lengthy, and annoying to write.

For example, this is a script without variables.

while true do
	game.Workspace.Obby.Obby1.ObbySet1.Obstacle1.Transparency = game.Workspace.Part.Transparency
	wait(45)
	game.Workspace.Obby.Obby1.ObbySet1.Obstacle1.Transparency = 0.75
	wait(1)
	game.Workspace.Obby.Obby1.ObbySet1.Obstacle1.Transparency = game.Workspace.Part.Transparency
	wait(35)
	game.Workspace.Obby.Obby1.ObbySet1.Obstacle1.Transparency = 0.5
	wait(1)
end

Jeez, what a long script! We could sure simplify that with some variables. To replace that with a variable, we could assign a variable to that obstacle, the part with the transparency that we want to set it to, the wait time, and the transparency!

This is that script with those variables assigned:

local Obstacle = game.Workspace.Obby.Obby1.ObbySet1.Obstacle1
local SetObject = game.Workspace.Part
local ST = 0.75 -- SetTransparency
local WT = 45 -- WaitTime
local WTT = 1 -- WaitTimeTwo

while true do
	Obstacle.Transparency = SetObject.Transparency
	wait(WT)
	Obstacle.Transparency = ST
	wait(WTT)
	Obstacle.Transparency = SetObject.Transparency
	wait(WT - 10)
	Obstacle.Transparency = ST - 0.25
	wait(WTT)
end

Viewer: Great, Nick. We made a script that only shows what the difference is between using a variable and not.

Nick: Not just that, we also recognized the importance of variables. And if you didn't already know how to make a variable, it was right there in the script. Hence:

 local Obstacle = game.Workspace.Obby.Obby1.ObbySet1.Obstacle1; 
 local SetObject = game.Workspace.Part;
 -- etc.

Now we may get into tables.

As aforementioned, tables are very useful in any Roblox game. They are just as useful as variables.

We denote tables mainly by variables, shown here:

local Table = {} -- Our table

We can then insert values and items into the table.

local Table = {game.Workspace.Sound1,
		       game.Workspace.Sound2} -- Our table

Often times, people will set a variable to a math.random() function that then will get a random value or item out of the table. Shown here:

local Table = {game.Workspace.Sound1,
		       game.Workspace.Sound2} -- Our table

local  MathRandom = math.random(1, 2) -- Gets a random number

To then get the actual item out of that table, and for this instance, play it, we'll need to make a function. We do that by saying, "[]", like so:

> I feel that it is important to mention that the number in the brackets will determine which value/item that we are retreiving from the table. So if we said, "Table[2]", we'd get, "game.Workspace.Sound2", instead of "game.Workspace.Sound1".

local Table = {game.Workspace.Sound1,
		       game.Workspace.Sound2} -- Our table

local  MathRandom = math.random(1, 2) -- Gets a random number

Table[MathRandom]:Play()

This script will play one of two sounds we have in Workspace.

Conclusion

Tables are assigned with variables. They are a necessity in any Roblox game, and are intricate, yet easy to make and understand. I hope you got something out of this lesson, whether that is the importance of a variable, what a table is, or what you can do with a table. Thank you for reading this guide.

Authored by: nick_namous ;)

Med Written By nick_namous

See nick_namous's profile on Roblox

Discussion