Storing questions, wrong answers, and correct answer

You can extend my Util function to return the named index (key).

function Utils.getEntryByIndex(entries, index)
	local pos = 0
	for key, entry in entries do
		pos += 1
		if pos == index then
			return entry, key
		end
	end
end

And then in another script:

local randomQuestionIndex = math.random(1, Utils.getCountOf(questions))

local choices, question = Utils.getEntryByIndex(questions, randomQuestionIndex)
Utils.shuffle(choices) -- Randomize order of choices

print(question, choices)
1 Like

How can I get it from printing out like this?
image

Turn off “Show Memory Address for Tables” in the output window.

It’s off.
image
image

Did you try turning off “Log Mode” as well?

How can I retrieve all the choices in text format? Right now print(choices[1]) prints out as nil

Use Utils.getEntryByIndex() to retrieve the entry by numerical index, this is because you are trying to get 1 from this:

{
	["55"] = false,
	["42"] = false,
	["66"] = true,
	["33"] = false,
}

But 1 doesn’t exist in here, only "55", "42", "66", and "33". Therefore:

local choice = Utils.getEntryByIndex(choices, 1)

I would strongly recommend learning basics of how tables work in Lua.

This prints out true or false, I need it to print out the number/string answer

Yes, I will certainly try once I have the time to do so

Sorry, I forgot that it returns the value. You can get the second returned value which is the key.

local isCorrect, choice = Utils.getEntryByIndex(choices, 1)
1 Like

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