Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Say I wanted to index a table within a table, how would I do that?

Asked by
PolyyDev 214 Moderation Voter
6 years ago

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

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
for i,v in pairs(ids) do
    print(i .. " = id = " .. v.id .. ", value = " .. v.value)
end
0
Thanks PolyyDev 214 — 6y
0
Is there a way to select them randomly PolyyDev 214 — 6y
Ad
Log in to vote
0
Answered by
H4X0MSYT 536 Moderation Voter
6 years ago

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.

0
You can't index a dictionary with an integer since the index is now a string. i.e ["Sparkletime Valkyrie"], ["The Classic ROBLOX Fedora"] PreciseLogic 271 — 6y
0
I have other randomizer scripts now PolyyDev 214 — 6y
Log in to vote
0
Answered by
PolyyDev 214 Moderation Voter
6 years ago
Edited 6 years ago

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.

Answer this question