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.
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
short answer: not all tables are dictionaries, but all dictionaries are tables
oh and yea @nicemike40 and @domboss37 pretty much explained everything else
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.
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.
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
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.
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!