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 » Scripters
Home Search
 

How to get a random entry in a table with requirements?

Previous Thread :: Next Thread 
ITBV is not online. ITBV
Joined: 27 Dec 2013
Total Posts: 954
15 Nov 2017 12:13 PM
So I have a table like this;

materials = {
["mat1"] = {
["rarity"] = 1,
},
["mat2"] = {
["rarity"] = 1,
},
["mat3"] = {
["rarity"] = 2,
},
}

What I'm trying to do is go through the table and get a random material, but only get that material if the rarity is 2 (This is just an example).

I'm confused how I would be able to do this, if anyone could give me some assistance that would be helpful.
Report Abuse
Proxinable is not online. Proxinable
Joined: 12 Sep 2011
Total Posts: 395
15 Nov 2017 12:15 PM
I would do it like this:

Loop through the table, save materials with a rarity of 2 to a second (temporary) table. Then choose a random one from that table.
Report Abuse
ITBV is not online. ITBV
Joined: 27 Dec 2013
Total Posts: 954
15 Nov 2017 12:16 PM
Ah that could work cheers.
Report Abuse
128Gigabytes is not online. 128Gigabytes
Joined: 17 Apr 2014
Total Posts: 3616
15 Nov 2017 01:53 PM
an alternate way would be to use repeat in a way such as this


local a = {}

for x = 0, 9, 1 do
a[#a + 1] = x;
end --[[Had to do this to get passed the stupid filter not allowing a table of numbers.]]

local selected;
repeat
selected = a[math.random(1, #a)]
until (selected >= 5)

print(selected)

I'm not saying you should do this instead but Im bored so here you go
Report Abuse
ITBV is not online. ITBV
Joined: 27 Dec 2013
Total Posts: 954
15 Nov 2017 02:05 PM
That's the way I was thinking of originally but wasn't exactly sure how to write it, Cheers!
Report Abuse
helloburp is not online. helloburp
Joined: 26 Aug 2011
Total Posts: 14376
15 Nov 2017 02:34 PM
I was given an algorithm for this when I asked about this earlier

paste blin

/Zf9uSAb3
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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