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

How can I save multiple things in a table?

Asked by
ItsMeKlc 235 Moderation Voter
8 years ago

So I'm trying to make a music player, and I have a song list local songs = {"268763467","261244793","142570249","165508381"} but I also want to save the title of each song, how would I do that?

1 answer

Log in to vote
2
Answered by
Legojoker 345 Moderation Voter
8 years ago

Use dictionaries. Dictionaries are an easy way to modify tables so that each item has its own unique name (kind of like how each word in a dictionary has its own definition). For example:

local songs = {
Friday = "268763467",
["The Song of the Cebu"] = "142570249",
Problem = "261244793",
Bills = "165508381"
}

The reason the song with multiple words is in [" "] is because the dictionary will only identify the string without erroring if there are no spaces or illegal characters. However, one-worded titles are allowed without any modifications. To iterate through your dictionary, just write something like this:

for title, songID in pairs(songs) do
    print(title, songID)
end

Your array currently has index values that are integers from 1 to the length of the table. Dictionaries modify the index.

Ad

Answer this question