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

how can i make a system that randomly picks a table out of a table?

table:

local questionlist1 = {
	question10 = {
		text = "What is your username";
		speed = .1;
		answer = 1;
		answertext1 = player.Name;
		answertext2 = string.lower(player.Name);
		successtext = "Correct!";
		failtext = "Wrong.";
		successeventfunction = function() learnedquestionsansweredright = learnedquestionsansweredright + 1 end;
		faileventfunction = function() end;
	};
	question11 = {
		text = "What is your account age";
		speed = .1;
		answer = 2;
		answertext1 = tostring(player.AccountAge - 1);
		answertext2 = tostring(player.AccountAge);
		successtext = "Correct!";
		failtext = "Wrong.";
		successeventfunction = function() learnedquestionsansweredright = learnedquestionsansweredright + 1 end;
		faileventfunction = function() end;
	};
	question12 = {
		text = "What is the name of this game";
		speed = .1;
		answer = 2;
		answertext1 = "the test.";
		answertext2 = "The test.";
		successtext = "Correct!";
		failtext = "Wrong.";
		successeventfunction = function() learnedquestionsansweredright = learnedquestionsansweredright + 1 end;
		faileventfunction = function() end;
	};
	question13 = {
		text = "What is the creator of this game";
		speed = .1;
		answer = 1;
		answertext1 = "karbis";
		answertext2 = "Karbis";
		successtext = "Correct!";
		failtext = "Wrong.";
		successeventfunction = function() learnedquestionsansweredright = learnedquestionsansweredright + 1 end;
		faileventfunction = function() end;
	};
	question14 = {
		text = "What is 3^3 + 1592 + 3295?";
		speed = .1;
		answer = 2;
		answertext1 = "4614";
		answertext2 = "4914";
		successtext = "Correct!";
		failtext = "Wrong.";
		successeventfunction = function() learnedquestionsansweredright = learnedquestionsansweredright + 1 end;
		faileventfunction = function() end;	
	};
	question15 = {
		text = "What is the square root of 9 + 1912 - 1842";
		speed = .1;
		answer = 2;
		answertext1 = "63";
		answertext2 = "73";
		successtext = "Correct!";
		failtext = "Wrong.";
		successeventfunction = function() learnedquestionsansweredright = learnedquestionsansweredright + 1 end;
		faileventfunction = function() end;	
	};
	question16 = {
		text = "What is 44% of 22 x 1392 x 9";
		speed = .1;
		answer = 1;
		answertext1 = "121271.04";
		answertext2 = "91271.04";
		successtext = "Correct!";
		failtext = "Wrong.";
		successeventfunction = function() learnedquestionsansweredright = learnedquestionsansweredright + 1 end;
		faileventfunction = function() end;	
	};
	question17 = {
		text = "1281 - 1823 / 1928 /(:) 8";
		speed = .1;
		answer = 2;
		answertext1 = "903.88180757";
		answertext2 = "1280.88180757";
		successtext = "Correct!";
		failtext = "Wrong.";
		successeventfunction = function() learnedquestionsansweredright = learnedquestionsansweredright + 1 end;
		faileventfunction = function() end;	
	};
}

thing that im trying to make it randomly pick:

local questionpicked = questionlist[math.random(1,#questionlist)]

1 comment

0

The math.random(1, #tbl) works to get a random element from ARRAY part of a table.

1 answer

✓ Accepted answer

You are looping through the table as if it were like this

{"a","b","c"}

while really the table you are dealing with is like this

{a="a",b="b",c="c"}

When you are dealing with key=value tables, there is a different way you need to loop it. I usually loop through tables like this with the table.foreach function but there are other ways to do it.

table.foreach(questionlist,function(i,j)
	print(i,j)
end)

So if you wanted to make a system that picked a random question from the list, first you would have to get all of the keys from the original list and put them into their own list.

items = {}
table.foreach(questionlist,function(i)
	table.insert(items,i)
end)

After that, you need to get a random element from that table and then receive that item from the original table by doing this:

question = questionlist[items[math.random(1,#items)]]

The final code should look like this:

items = {}
table.foreach(questionlist,function(i)
	table.insert(items,i)
end)
question = questionlist[items[math.random(1,#items)]]
print(question)

You can read more about tables here. If I need to clarify something or if there is an error tell me and I will be happy to help.

2 comments

0

thanks but i realised i did a dictionary instead but i will use this when i need to

0

-1 for using deprecated table.foreach and encouraging it

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