How to find table in a table?

Hello, I am making a trivia game, and I want to know how to find a table in a table. I tried doing it but it gives me attempt to index nil with ‘answers’. Can someone help? Here is my current code:

--Module script
local data = {
	{
		question = "Name an item a multi-millionare would buy",
		answers = {"Yacht", "Superyacht", "Megayacht", "RealEstate", "Lamborghini", "Bugatti", "House", "Home", "Mansion", "PrivateJet", "Chef", "Maid"},
	},
	{
		question = "Name a country in Eastern Europe",
		answers = {"Russia", "Ukraine", "Belarus", "Romania", "Moldova", "Finland", "Estonia", "Latvia", "Lithuania"},
	}
}

return data


--server script inside another script inside server script service
local Status = game.ReplicatedStorage.Status
local data = require(script.Parent.Parent["Questions/Answers"])
local Question = Status.Value
local plrs = {}

game.ReplicatedStorage.SendAnswer.OnServerEvent:Connect(function(player,answer)
	local StepsFolder = workspace.StepsFolder
	local questionnum = Status.Value
	local answerz = table.find(data[answer])
	
	if table.find(data[questionnum].answers, answer) then --Keeps giving me error here: attempt to index nil with 'answers'
		print("Answer found!")
		local length = #answer
		string.lower(answer)
		for i = 1,length do
			local step = Instance.new("Part")
			step.Parent = StepsFolder
			step.Anchored = true
			step.Color = Color3.new(0.901961, 1, 0.576471)
			step.Name = "testingStep"
			step.Size = Vector3.new(5,1,5)
			step.Position = player.Character:FindFirstChild("HumanoidRootPart").Position - Vector3.new(0, 2, 0 )
			if i == length then
				game.ReplicatedStorage.TweenTransparency:FireClient(player,length)
			end
		end
	end
end)
1 Like

make sure you label the Tables, You are pretty much trying to get a Table within an unnamed Table

Table = {
["Example"] = {["Stuff"] = {}}

}

table.find(Table["Example"]["Stuff"])
1 Like

You can find it via an index. For example:

local table1 = {
	{"Example"};
	{"Example2"};
}

--Since there are 2 tables inside this table, we can refer to the table using an index.

print(table1[1][1]) -- This would print out "example"

print(table1[2][1]) -- This would print out "example2" since it is referring to the second index of table1

This is confusing for me to understand with my situation. Can you explain it a bit more in my situation?

Pretty much what im saying is do this

local data = {
	[1] = {
		question = "Name an item a multi-millionare would buy",
		answers = {"Yacht", "Superyacht", "Megayacht", "RealEstate", "Lamborghini", "Bugatti", "House", "Home", "Mansion", "PrivateJet", "Chef", "Maid"},
	},
	[2] = {
		question = "Name a country in Eastern Europe",
		answers = {"Russia", "Ukraine", "Belarus", "Romania", "Moldova", "Finland", "Estonia", "Latvia", "Lithuania"},
	}
1 Like

Then how do I find it now in the server script?

You don’t need to do that. You can just refer to the index. For example:

local data = {
	{
		question = "Name an item a multi-millionare would buy",
		answers = {"Yacht", "Superyacht", "Megayacht", "RealEstate", "Lamborghini", "Bugatti", "House", "Home", "Mansion", "PrivateJet", "Chef", "Maid"},
	},
	{
		question = "Name a country in Eastern Europe",
		answers = {"Russia", "Ukraine", "Belarus", "Romania", "Moldova", "Finland", "Estonia", "Latvia", "Lithuania"},
	}
}

print(data[1].question) -- The [1] refers to the index (First table)
2 Likes

But i am trying to get the answers, which is in a subtable, or a table in the data table.

That’s exactly what it does, it gets the table. It get’s the dictionary inside the table with the index 1. (Try it by printing or smth)

1 Like

if you want a random Question, You can do this:

local RandomNum = data[math.random(1, #data)]
print(RandomNum.question)
1 Like

Thank you, but I already have a random question generator, what I am asking is how would I find if the answer matches up with the players answer. Like this picture above ^^

Don’t use table.find, use data[questionnum].answers.

1 Like

But then how would I check what the player answered and see if it matches up?
image

So what exactly are you using?

  • The Chat?

  • A TextBox?

  • A TextButton?

1 Like

What do you mean?

fdssfddsfsfdfdssfdfsddsffsdsfdsfd

What is the questionnum if you print it?

1 Like

wdym?

What are you using to answer the questions?

1 Like

Questionnum is a string. Its basically the question the player is guessing.

You can use a for i, v in pairs() loop as an alternative way to do that.

1 Like