Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer
for i,v in pairs(ids) do
	print(i .. " = id = " .. v.id .. ", value = " .. v.value)
end

2 comments

0

Thanks

0

Is there a way to select them randomly

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.

2 comments

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"]

0

I have other randomizer scripts now

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.