Modulescript entries

Hello, I am making a trivia/guessing type of game and I want to know if it is possible to assign 2 answers to 1 question in a module script. For example,
image

My handwriting is terrible, I know. By the way here is the luau formatted version of the code incase you want to make some changes to it to show me:

local module = {
	["Name an US President"] = "barack obama",
	["Name an Animal"] = "dog",
	
}

return module

Put them into a table.

local module = {
	["Name an US President"] = "barack obama",
	["Name an Animal"] = {"dog", "cat", "bird"},
	
}

return module
1 Like

How would I access them now? Currently I have

if answer == data[Question] then

Would this still work?

if table.find(data[Question], answer) then
1 Like

How would I get a random question from the module script?

local randomQuestion = data[math.random(1, #data)]

Whenever I try that it gives me an error saying: invalid argument #2 to ā€˜randomā€™ (interval is empty)

The problem with their code is that #data only returns the amount of things with a numbered index, which anything using a string as the index wouldnā€™t be counted. Hereā€™s a fix:

local module = {
	{
		question = "Name an US President",
		answers = {"barack obama"},
	{
		question = "Name an Animal",
		answers = {"dog", "cat", "bird"},
	},
}

Now, we will need to change the code a bit:

local questionnum = math.random(#data)
local questionobj = data[questionnum]

--then for checking whether or not they had a correct answer
if table.find(questionobj.answers, answer) then
1 Like

Thank you except theres a problem, whenever I put the questionnum variable as the question text, it is only ā€œ1ā€ and it somehow prevents my script from going any further.

Make sure to set the question as questionobj.question

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.