Example. If I wanted a script to pull a random array out of this dictionary and print it, how exactly would I do that?
local ids = { ["Sparkletime Valkyrie"] = {id = 1180433861, value = 10000}, ["The Classic ROBLOX Fedora"] = {id = 1029025, value = 100000} }
if I use this:
for key,value in next, ids do print(key," = ",value) end
to print the arrays out as you probably know it will output:
The Classic ROBLOX Fedora = table: 2A6741FC
Sparkletime Valkyrie = table: 2A67422C
So how do I make it print
The Classic ROBLOX Fedora = id = 1180433861, value = 10000
Sparkletime Valkyrie = id = 1029025, value = 100000
or something along those lines. Any help would be appreciated
for i,v in pairs(ids) do print(i .. " = id = " .. v.id .. ", value = " .. v.value) end
Suprised the other answer was accepted. Didn't cover randomization, so I will. Doing
#ids
will actually return the ammount of entries in there.
local Table = {1, 9, 91, 4} print(#Table) -- > 4
So, combining that with math.random,
local ids = { ["Sparkletime Valkyrie"] = {id = 1180433861, value = 10000}, ["The Classic ROBLOX Fedora"] = {id = 1029025, value = 100000} } function random() local Selected = ids[math.random(#ids)] print(Selected .. " = id = " .. Selected.id .. ", value = " .. Selected.value) end
Basically, we chose a random number between 0, and the ammount of entries in the table ids. We chose the entry that has the entry number that we randomly selected, and get its values.
If you want to know these randomize scripts, here they are:
function randomKey(d) local t = {}; for k, v in pairs(d) do table.insert(t, k); end return t[math.random(#t)] end
Just prints the random key
Say you wanted to click a button and access the id and value of the key? Here is a function for that
local t = {} for k, v in next, yourtable do t[#t+1] = k end local key = t[math.random(1,#t)] local value = yourtable[key].yourvalue local id = yourtabletable[key].yourvalue
Put that in a function you want it in.