I’m trying to get a random value out of a table I had that includes seats and checks which is available and not however when I try to get it I get an error saying bad argument #2 to ‘random’ (interval is empty)
The issue is that this is a dictionary, dictionaries use keys and not numbered indexes.
And, if I’m not mistaken, using the # operator on a dictionary returns 0 (as it returns the number of indexes, I think)
But, there’s an easy way to get this to work with your current scenario:
local index = math.random(1, 12) --//Pick a random number because there are 12 seats available
local the_seat = seats["seat"..index] --//Access the key in the dictionary based on number drawn
--//Do stuff with 'the_seat'
You can though. the key variable is the key that was randomly chosen from the table. If at any point you want to check and see what that key is pointing to in the table, just read from the table with that key:
You could use parallel arrays, which just means two separate tables with corresponding indices in them.
So an example could be:
local customers = {"Ron", "Jack"};
local customerBalances = {45, 92};
local customerIndex = 1
local currentCustomer = customers[customerIndex]
local currentCustomerBalance = customerBalances[customerIndex]
--//It's called parallel because the values in both tables refer to the same index
Another thing you could do is use nested tables (tables within tables) to store multiple values at each index.
local nestedTable = { {1, 2, 3} }
local someTable = nestedTable[1]
print(someTable[1])