Am I referencing my table wrong

for i,v in pairs(Table[“results”][CurrentQuestion][“incorrect_answers”]) do
Table.insert(OptionTable,v)
end

Would need to know what the structure of Table looks like in order to help you figure out if you’re referencing it wrong. Are you getting any errors or not seeing the behavior you’d expect and that’s why you’re asking?

On a side note, so long as the elements within a table are string values and do not have spaces or start with an invalid character, you can just access them using the ‘.’ as you would ascending and descending the game model hierarchy. Example:

Table["results"]["CurrentQuestion"]["incorrect_answers"]
--is the same as
Table.results.CurrentQuestion.incorrect_answers
3 Likes

As @East98 said, we need more information. You have nothing but a line of code and requested assistance. You could provide what the table looks like, any errors, everything else you have tried if you are passing the table through a remote event, or a bindable event, etc.

However, I notice that [CurrentQuestion] is not in a string, but instead a value.

Unless this is a spelling error, you aren’t checking if CurrentQuestion even exists in the table (Judging by the little code provided), in which this could be the result of your issue.

Screen Shot 2021-03-02 at 8.05.49 PM

So which line is line 11? (Still lacking a bit of vital context…)

Table[“results”][“CurrentQuestion”][“incorrect_answers”]

OH! WAIT! Hold up. So if you have a table named ‘Table’. Then to insert something into that table you need to do:

table.insert(Table, v)

table (lowercase) is a reference to the class containing functions for modifying tables, however tables by default do not all have methods for inserting

Could I see your full code? I’d be happy to help from there but I think you’ve got more than one tricky errors going on right now

Ok here is my main script called mainScript


local IntermissionModule = require(script.IntermissionModule)

local EndGame = require(script.EndGame)

local Table = {}

local OptionTable = {}

local CurrentQuestion = 1

for i,v in pairs(Table["results"][CurrentQuestion]["incorrect_answers"]) do

table.insert(OptionTable,v)

end

table.insert(OptionTable,#OptionTable+1,Table["results"][CurrentQuestion]["4"].CorrectAnswer)

warn(Table)

-- ASK QUESTIONS AND ALL RELATED TO ASKING AND STORING THE SCORE AND SHOWING STATISTICS

questionModule.GetQuestions()

local Table = questionModule.questionInfo

print(Table["results"][CurrentQuestion])

And my module called QuestionModule

local module = {}

function module.GetQuestions()
	print('Question asked')
	-- Remember to set enable HTTP Requests in game settings!
	local HttpService = game:GetService("HttpService")

	local function request()
		local response = HttpService:RequestAsync(
			{
			Url = "https://opentdb.com/api.php?amount=10",  -- This website helps debug HTTP requests
				Method = "GET",
				Headers = {
					["Content-Type"] = "application/json"  -- When sending JSON, set this!
				},
			}
		)

		-- Inspect the response table
		if response.Success then
			print("Status code:", response.StatusCode, response.StatusMessage)
			module.questionInfo = HttpService:JSONDecode(response.Body)
			warn(module.questionInfo)
			module.t = {}	
		
		return module.questionInfo			
		else
			print("The request failed:", response.StatusCode, response.StatusMessage)
		end
	end

	-- Remember to wrap the function in a 'pcall' to prevent the script from breaking if the request fails
	local success, message = pcall(request)
	if not success then
		print("Http Request failed:", message)
	end

end

return module

Alright, so the way you currently have your code set up, that for loop in questionModule will not do anything because your variable Table is an empty table when it runs.

On the line:

table.insert(OptionTable,#OptionTable+1,Table[“results”][CurrentQuestion][“4”].CorrectAnswer)

You don’t need to include the argument #OptionTable+1 because table.insert will automatically insert the element into the last position of the list.

(It would also help us out if you could put all your code segments into formatted code blocks by encasing them in lines with ``` )