How do I pick 1 random value from a table?

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

Try using this function I just created

local function chooseRandomFromDictionary(dictionary)
	local t={}
	for i,v in pairs(dictionary) do
		t[#t+1]=v
	end
	return t[math.random(1,#t)]
end

Edit: Changed i to v on line 4, my mistake

1 Like

I appreciate you trying to make some sense to me here. The confusing context of everything you’re saying is not getting through to me on a non-visual level, but I looked over your code before, and I want to see if I got this all right in my head now

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

The above code sample creates a new dictionary that holds the value of the key from my ItemTable

local choiceIndex = math.random(1,#keys)

From what @domboss37 said earlier, this will make a math.random() sequence from the first part of the table, to the end of the key table (which contains all the locations of my hats)

local choiceKey = keys[choiceIndex]

The math.random() call from earlier is used to assign a number in the table since tables go by numbers based on how many values their are

local choice = ItemTable[choiceKey]

This will then take the number that was found in the keys table, and use it to find the exact position in my ItemTable, then assigning whatever the value is to a variable*.

Now if I wanted to set this to a datastore I could write this choice variable and concatenate with the name of a datastore to check if they have already gotten this item before. Sorry if that doesn’t make much sense but it does to me.

Am I right on some things here?

This function converts the dictionary to a table, which allows you to index by number.

I tested it using the following code

mytable={hi="hiiii",bye="byeeeee"}
print(chooseRandomFromDictionary(mytable))

And it either outputs “hiiii” or “byeeeee”

no it doesn’t create a new dictionary

usually when people want to save dictionaries, they turn it into a string and save that string
they then turn the string into a table when you join back

I think I get the gist of things here now. It’s still quite vague for me but hopefully the more I use what @nicemike40 said the more I’ll come to understand it. I need a break as this is just confusing from all the different points of views here. I was able to get it to print the name of that hat at randomly every time the tool is used, and that’s all I needed here. Thank you all for your help.

Pretty close! Here, let me annotate what the variables are at each step.

local keys = {} -- this will eventually be an array
                -- full of keys: { "Hat1", "Hat2", "Hat3" }

-- ...so let's build it:
for k in pairs(ItemTable) do -- `k` will be "Hat1", then "Hat2", then "Hat3"
  table.insert(keys, k) -- put them all into the `keys` array
end

-- at this point in the script:
-- keys = { "Hat1", "Hat2", "Hat3" }

-- (this is an array, not a dictionary, so #keys will work properly and give us 3)

local choiceIndex = math.random(1,#keys) -- some random number between 1 and 3,
                                         -- for example 2

local choiceKey = keys[choiceIndex] -- use that number to pick a random key,
                                    -- for example choiceKey = "Hat2" if
                                    -- choiceIndex == 2

local choice = ItemTable[choiceKey] -- use that key (e.g. "Hat2") to access the
                                    -- original table. So this line is just doing
                                    -- ItemTable["Hat2"]

A lot of people have suggested doing ItemTable[math.random(#ItemTable)] or something similar, but that won’t work, as you’ve figured out.

15 Likes

This helps quite a bit. I know this is sort of extra and stuff, and it probably strays from the original topic title, but now that I have this all setup, and am able to print the value I get from my original table, another question of mine that I wasn’t too concerned about until after this was finished was a way to check the value and give the hat without having to elseif chain, since I see myself doing that a bit too much with things and I know it’s not clean whatsoever.

Also I’ll switch the solution to that post as it’s the same as your original solution, but explained better

Sorry, what’s your other question? I missed it :slight_smile:

I was just wondering if there is a more professional way of making it so I can check and give the hat based on what the random hat that was given from the choice variable instead of using a long elseif chain.

edit this is why I said it may be something a little more different from the actual topic title so I don’t know if I’m fully allowed to ask that question here as well.

Do you have an example of what you mean by a long elseif chain?

Once you have the choice, you can just use it directly as if it were any other variable:

local clone = choice:Clone()
clone.Parent = game.Players.LocalPlayer.Character

Oh sorry I got a bit confused there. I forgot since this saves the original path of the where I save my hats, it will work normally if I were to just use choice to clone to the character. The actual way I’m making this system is by making it so when the player receives that specific hat, it will save the hat they got, and I thought I would need elseif chains for this whole thing to work. Apologies!

1 Like