How do I print stuff from a dictionary table in order?

  1. What do you want to achieve? Keep it simple and clear!
    I want to print the values for questions 1 to 7 in order.
local questions = {["question1"] = {question = "this is \"quest1\"", answer = "wat" },  
                                --wana print the ^^ value from questions 1 to 7 in order.
["question2"] = {question =  "this is \"quest2\"", answer = "is" }, 
["question3"] = {question = "this is \"quest3\"", answer = "going" },
["question4"] = {question = "this is \"quest4\"", answer = "on" },
["question5"] = {question = "this is \"quest5\"", answer = "in" },
["question6"] = {question = "this is \"quest6\"", answer = "banana" },
["question7"] = {question = "this is \"quest7\"", answer = "land" }}





return questions




for i = 1, #questions do
print(questions["question"..tostring(i)].question)
end
1 Like
for i = 1, #questions do
    local QuestionTable = questions["question"..tostring(i)]
    print(QuestionTable.question.."\n"..QuestionTab.answer)
end
1 Like
local sss = game:GetService("ReplicatedStorage")
local quest = require(sss.question) 

for i = 1, #quest do
	print(quest["question"..tostring(i)].question)
end

i may be bananas, but nothin printed

no errors?, you could try an in pairs loop if you want

local rs = game:GetService("ReplicatedStorage")
local quest = require(rs.question)

for i, v in pairs(quest) do
    local QuestionTable = v
    print(QuestionTable.question.."\n"..QuestionTab.answer)
end
1 Like

I just edited it so use the one I just finished

1 Like

Thanks for trying.
Printed in order question 1, 6, 7, 5, 2, 3, 4

local rs = game:GetService("ReplicatedStorage")
local quest = require(rs.question)

for i, v in pairs(quest) do
	local QuestionTable = v
	print(QuestionTable.question.."\n"..QuestionTable.answer)
end


use ipairs my bad

it should be in order then

1 Like
local rs = game:GetService("ReplicatedStorage")
local quest = require(rs.question)

for i, v in ipairs(quest) do
	local QuestionTable = v
	print(QuestionTable.question.."\n"..QuestionTable.answer)
end
1 Like

This would probably be how you do it.

local i = 0
for _ in pairs(questions) do
	i += 1
	local QuestionTable = questions["question" .. tostring(i)]
	print(QuestionTable.question .. "\n" .. QuestionTable.answer)
end

I would still prefer if you stored these questions in a regular old array!

local questions = {
	[1] = {question = "this is \"quest1\"", answer = "wat" },  
        -- wana print the ^^ value from questions 1 to 7 in order.
	[2] = {question =  "this is \"quest2\"", answer = "is" }, 
	[3] = {question = "this is \"quest3\"", answer = "going" },
	[4] = {question = "this is \"quest4\"", answer = "on" },
	[5] = {question = "this is \"quest5\"", answer = "in" },
	[6] = {question = "this is \"quest6\"", answer = "banana" },
	[7] = {question = "this is \"quest7\"", answer = "land" }
}

Then you would be able to use an ipairs/numerical loop like all these other posts:

for i, QuestionTable in ipairs(questions) do
	print(QuestionTable.question .. "\n" .. QuestionTable.answer)
end

-- or

for i = 1, #questions do
	local QuestionTable = questions[i]
	print(QuestionTable.question .. "\n" .. QuestionTable.answer)
end
1 Like


image
Still same.

no you didn’t use ipairs like I told you to

1 Like

When i tried ipairs it didnt print anything.

oh yea non-numbers my bad

idk why the first reply on this didn’t work tho, anyways it’s good you got a solution now

1 Like


image
Worked, thanks. :grin:

1 Like

Thanks for trying though. :smile:

Thanks for the advice. I will try it out later.

1 Like

I like this so im usin it from now on. :slightly_smiling_face:
image

image

1 Like

Very very very useful. :smirk:

1 Like