Learning Tables Right Now... Is there a better way to write them?

I’m currently learning tables on roblox, this seems very messy and I’d like to know if there’s a better way to write these before I dig myself into bad coding habbits? Formatting, ordering, whatever makes it look nicer and easier to read.

1 Like

try using a for loop, that’ll be easier

I’m not trying to use anything,
I said I’m trying to learn how to use tables.
I’m asking if there’s better ways to format…

I don’t think so, the best way to organize things as far as I know is dictionaries, basically tables but you put a key (like a name of some sort)

local dict = {
hello = 5
world = 1
}
print(dict[hello]) --prints 5

I mean, that right there is a good way to write it, but you could also try to add some space to in a way show that the values are inside of the table

local Table3 = {
    Value,
    Value,
    Value
}

It all depends on the usecase if you wanna use dictionaries or arrays/tables

Yeah it’s just formatting you can follow robloxs style guide, don’t forget to indent, use that auto formatter.

Add trailing commas in multi-line tables.

This lets us re-sort lines with a single keypress and makes diffs cleaner when adding new items.

local frobs = {
    andrew = true,
    billy = true,
    caroline = true,
}

https://roblox.github.io/lua-style-guide/#tables

2 Likes

Perhaps you can try out metatables, it’s almost the exact same as tables!

make sure to tab each element, so instead of

local Table3 = {
"ONE",
"TWO",
"THREE",
"FOUR",
}

write

local Table3 = {
    "ONE",
    "TWO",
    "THREE",
    "FOUR",
}

it’s also a good practice to put a comma after each element in the table (even the last one), this is mentioned in the Lua style guide as well. I recommend you take a look at the table section of it to see more

Edit: mb I didn’t see dthecoolest’s reply, he pretty much wrote what I did

3 Likes