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:
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.
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).
You can index a value in a table like this: table[number]
e.g:
local example = {"eggs", "bread", "potatoes"]
print(example[1]) -- 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 = {
[1] = "eggs",
[2] = "bread",
[3] = "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
@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
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