Problems with TextBoxes and Tables

I’m attempting to make a GUI that uses the same TextBox and TextLabel, so the answers are stored in a table, now I’m giving them the option to go back, which will take their answer from the table and put it in the TextBox.

That works fine.

The problem is going forward after going backwards.

If you try to use the next button it overwrites all the future table answers, with what is currently in the TextBox.

currentquestion = 1
saftey = false

questions = {"example1", "example2", "example3", "example4","example5",}
answers = {}

text = script.Parent.TextLabel

script.Parent.next.MouseButton1Click:Connect(function()
	if saftey == false then
		saftey = true
		print(script.Parent.TextBox.Text .. currentquestion)
		table.insert(answers, currentquestion, script.Parent.TextBox.Text)
		currentquestion = currentquestion + 1
		text.Text = questions[currentquestion]
		print(script.Parent.TextBox.Text .. currentquestion)
		if answers[currentquestion] ~= nil then
			script.Parent.TextBox.Text = answers[currentquestion]
		else
			script.Parent.TextBox.Text = ""
		end
		print(script.Parent.TextBox.Text .. currentquestion)
		wait(0.1)
		saftey = false
	end
end)

So, could someone let me know exactly what I may be doing wrong?

I can provide more if needed.

Try this.

currentquestion = 1
saftey = false

questions = {"example1", "example2", "example3", "example4","example5",}
answers = {}

text = script.Parent.TextLabel

script.Parent.next.MouseButton1Click:Connect(function()
	if saftey == false then
		saftey = true
		print(script.Parent.TextBox.Text .. currentquestion)
		answers[currentquestion] = script.Parent.TextBox.Text
		currentquestion = currentquestion + 1
		text.Text = questions[currentquestion]
		print(script.Parent.TextBox.Text .. currentquestion)
		if answers[currentquestion] ~= nil then
			script.Parent.TextBox.Text = answers[currentquestion]
		else
			script.Parent.TextBox.Text = ""
		end
		print(script.Parent.TextBox.Text .. currentquestion)
		wait(0.1)
		saftey = false
	end
end)

While yes, you could use table.insert, if you want the effect where it only overwrites the current question, you’ll have to manually index it as seen in the above example.

Complete life saver! Didn’t even think of doing it like that.

1 Like