Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

does local table = {} mean empty?

Asked by
Grazer022 128
3 years ago

while learning how to code, I have kept seeing some people use a table without anyone inside like this:

local table = {}

i keep on wondering and thinking if that means it’s empty or there are something in it. Also i wonder on what those are used for. Any help would be great, thanks!

0
yep laurenbtd5 379 — 3y

1 answer

Log in to vote
1
Answered by
Yozoh 146
3 years ago

Yes, this is an empty table. But you should not name a table "table", because that is a special keyword on roblox. Any text that turns blue is a special keyword you should not use to name anything in your code. Tables are basically arrays or a list. They can store information like strings, booleans, functions, and more. Usually people use them to store items in a player's inventory.

local myTable = {} -- Empty
table.insert(myTable, "Oranges") -- Inserting the string "Oranges"
print (myTable[1]) -- Printing the first item in our table: Oranges
-- Inserting Apples, Grapes, and Bananas
table.insert(myTable, "Apples")
table.insert(myTable, "Grapes")
table.insert(myTable, "Bananas")
table.remove(myTable, 3) -- removing the 3rd item in our array/table/list: Bananas

for i, v in pairs(myTable)do -- printing every item in our list
    print (i, v)
end
0
Output of this code will be: Oranges 1 Oranges 2 Apples 3 Bananas Yozoh 146 — 3y
0
what are empty tables used for anyways? Grazer022 128 — 3y
0
they are used for if you need to insert other stuff into the table later, or clear a table for other reasons. ChicaLower 25 — 3y
0
ahhh. Thanks again! Grazer022 128 — 3y
Ad

Answer this question