generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Accessing dictionaries?

Previous Thread :: Next Thread 
HyperProduct is not online. HyperProduct
Joined: 11 Nov 2012
Total Posts: 3362
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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
20 Jul 2014 10:31 PM
ex.Example1
Report Abuse
HyperProduct is not online. HyperProduct
Joined: 11 Nov 2012
Total Posts: 3362
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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
20 Jul 2014 10:36 PM
ex['Example' .. math.random(2)]
Report Abuse
HyperProduct is not online. HyperProduct
Joined: 11 Nov 2012
Total Posts: 3362
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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
20 Jul 2014 10:44 PM
...Why are you doing it like that, if you want a random element?
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
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
HyperProduct is not online. HyperProduct
Joined: 11 Nov 2012
Total Posts: 3362
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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
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
HyperProduct is not online. HyperProduct
Joined: 11 Nov 2012
Total Posts: 3362
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
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image