# How do I pick 1 random value from a table?

I’m trying to make a system where a player opens up a crate using a tool. My animation plays nicely, and it will pick 1 random value through this table here:

``````-- Item Table --
local ItemTable = {
Hat1 = game.ServerStorage.HatStorage.CrateHats.TestHat,
Hat2 = game.ServerStorage.HatStorage.CrateHats.RockHat,
Hat3 = game.ServerStorage.HatStorage.CrateHats.CakeHat
}
``````

and clone that specific hat to the players character model. I know how to use for loops, but I don’t know how to pick a random value from a table.

I already have a saving system setup for this, I just need help making it pick a random hat from this table then clone it.

Any help would be super helpful. A little stumped on this.

9 Likes

You could generate a random number with Random.new():NextInteger(1, #ItemTable) and then index an item in the table with the random number created.

1 Like

Thank you for the reply. I’m having a bit of trouble understanding that. I’ve used math.random() before for specifc things, but I don’t think I fully understand that syntax. A more friendly explanation of how that line of code works would really help my understanding. Much appreciated.

Make a list of the keys, then pick one.

``````local keys = {}
for k in pairs(ItemTable) do
table.insert(keys, k)
end

local choiceIndex = math.random(1,#keys)
local choiceKey = keys[choiceIndex]
local choice = ItemTable[choiceKey]
``````

You only have to do the first half once at the start of the game, then you can reuse the keys table (assuming the ItemTable doesn’t change).

Feel free to use Random.new or whatever.

2 Likes

Thank you. As mentioned before,

Would be a huge help on how it picks a random item and what the stuff you plug into that random.new() statement does.

Also thank you for fixing up my game.ServerStorage.HatStorage.CrateHats… spam. I noticed that, but just wanted to get help as soon as possible

actually I just realized another flaw in my script, I am not gonna add a third edit
you should look into @nicemike40’s script

you should look at this link Random | Roblox Creator Documentation

1 Like

You can index a value in a table like this: table[number]
e.g:

``````local example = {"eggs", "bread", "potatoes"]
print(example) -- would print "eggs" since it's the first value in the table, 1 is the index
``````

the table could also be visualized as

``````local example = {
 = "eggs",
 = "potatoes"
}
``````

For your particular example, the random number generated between 1 (the start of the table) and the end of the table (using #ItemTable) could be used as an index.
Oh yeah, and make sure you use Random.new():NextInteger() and NOT Random.new():NextNumber()
e.g:

``````local example = {"a", "b", "c"}
local randomNum = Random.new() :NextInteger(1, #example) -- let's say the generated number is 2
print(example[randomNum]) -- would print "b", since it's the second value
``````
1 Like

``````    local ItemTable = {
game.ServerStorage.HatStorage.CrateHats.TestHat,
game.ServerStorage.HatStorage.CrateHats.RockHat,
game.ServerStorage.HatStorage.CrateHats.CakeHat
}
``````

then generate the number like this:

``````local chosenNumber = math.random(1, #ItemTable)
print(ItemTable[chosenNumber])``````
2 Likes

@sec_dude I appreciate the help. You helped me understand this a lot better

@domboss37 You made this make all complete sense now. So basically if it were to loop through a table, every value in a table is like sorted by numbers?

So if I have a, b, and c in a table, and it does math.random() through that table, it will pick the spot in the table by number (lets say it picks 2, and 2 is b) it will print it’s value? If so that makes a lot more sense.

Yes that’s it it make a random number between 1 and the length of the table, then uses this to get the item based on the index (position in the table) which for lua starts at 1

just a quick note, if the table is a dictionary
then #Table doesn’t work because it will be equal to 0

1 Like

Yeah, I think I figured that out when I tried testing this, which means the way I was thinking about it isn’t exactly true. This now suddenly confuses me again. How does one create an actual table, or are dictionaries basically “tables”?

edit

I’m new to tables and stuff so please bare with me. I might not make much sense trying to understand this all.

Hope it helped though. Remember to mark the one that worked as solution

You could probably eliminate “Hat1”, “Hat2”, “Hat3”, unless you need them. In that case you could probably use this bit of code to create a separate table, assuming the table doesn’t change.

So last reply of the night the difference between a table and a dictionary is that dictionary’s have keys tables do not so like @sec_dude said you can remove the keys (“Hat1”, “Hat2”, “Hat3”) and the code I provided earlier should then work as a table

“tables” is a catch-all term for things that have `{ ... }` around them.

Those have an “array part”, which is things like `t1 = { "a", "b", "c" }`.

They also have a “hash table part” (or “dictionary”), which is things like `t2 = { d = 1, e = 2, f = 3 }`.

You can have both in the same table: `t3 = { "a", "b", "c", d = 1, e = 2, f = 3}`.

But `#tab` only works for the array part. So `#t3` above would return 3, not 6.

short answer: not all tables are dictionaries, but all dictionaries are tables
oh and yea @nicemike40 and @domboss37 pretty much explained everything else

pretty sure you could just do

``mytable[math.random(1,#mytable)]``

Not if it’s a dictionary is what we’ve been talking about

My bad, I was confusing this with something else