|
| 20 Jul 2014 10:30 PM |
Hey, I have a question about accessing dictionaries. I quickly made an example one:
ex = {["Example"] = game.Workspace.Example, ["Example2"] = game.Lighting.Example2}
Now I'm trying to access the first part of it ("Example", not the location) but I'm not sure how. I tried to access the whole thing:
currentEx = ex[math.random(1,#ex)]
but that didn't work. I even tried to do this:
currentEx = ex[math.random(1,#ex[1])]
but of course that didn't work, either. What would I do to access the first part of a value in a dictionary? Also, how would I access the second part? Thanks c: |
|
|
| Report Abuse |
|
|
| |
|
|
| 20 Jul 2014 10:34 PM |
| Notunknown, thanks! But how I would I make that work with the math.random, since I don't know what the value is going to be? |
|
|
| Report Abuse |
|
|
|
| 20 Jul 2014 10:36 PM |
| ex['Example' .. math.random(2)] |
|
|
| Report Abuse |
|
|
|
| 20 Jul 2014 10:43 PM |
That would work for this situation, but what about the actual dictionary that I am using?
songs = {["Happy"] = game.Lighting.Happy, ["Wake Me Up"] = game.Lighting.WakeMeUp, ["Roar"] = game.Lighting.Roar, ["Demons"] = game.Lighting.Demons, ["Let It Go"] = game.Lighting.LetItGo, ["It's Time"] = game.Lighting.Time, ["Rude"] = game.Lighting.Rude, ["Royals"] = game.Lighting.Royals, ["Countdown"] = game.Lighting.Countdown, ["Wrecking Ball"] = game.Lighting.WreckingBall, ["Nobody Compares"] = game.Lighting.NobodyCompares, ["Gangnam Style"] = game.Lighting.Gangnam, ["Everything Is Awesome"] = game.Lighting.Awesome}
How would I make it work with this? |
|
|
| Report Abuse |
|
|
|
| 20 Jul 2014 10:44 PM |
| ...Why are you doing it like that, if you want a random element? |
|
|
| Report Abuse |
|
|
|
| 20 Jul 2014 10:45 PM |
local indexedTable = {} for _, v in next, tab do indexedTable[#indexedTable + 1] = v end return indexedTable[math.random(#indexedTable)] |
|
|
| Report Abuse |
|
|
|
| 20 Jul 2014 11:06 PM |
| I do it like that because I'm trying to make it so that a GUI button's text changes to the song name, then the song plays at the location. Is there a way I can do it with both of those? I could make 2 arrays, but how would I make both arrays be random, while they equal the same song? So if the random song was Royals, then the other array would be game.Lighting.Royals. I don't want to make tons of if statements :/ |
|
|
| Report Abuse |
|
|
|
| 20 Jul 2014 11:07 PM |
local indexedTable = {} for _, v in next, tab do indexedTable[#indexedTable + 1] = v end return indexedTable[math.random(#indexedTable)]
That should do it |
|
|
| Report Abuse |
|
|
|
| 20 Jul 2014 11:22 PM |
| Okay, I will try it. Can you explain how it works? How would I change it to the string part instead of the location? |
|
|
| Report Abuse |
|
|
|
| 20 Jul 2014 11:25 PM |
| It puts all the values in a table with indexes, then use math.random on it :P |
|
|
| Report Abuse |
|
|