Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why can I not read values from this table?

Asked by 9 years ago

Hi. I have a table that stores questions and four possible answers, of which only one is correct. I found that I could create a table as follows and I found this code from a lua helpsite not specific to ROBLOX. The problem is, I can apparently read values from this table by printing GeneralKnowledge[1][2] to print "London"

GeneralKnowledge = {}
for i = 1, 4 do
    GeneralKnowledge[i] = { 0,0,0,0,0 }
end 
GeneralKnowledge[1] = {"What is 2+2?","2","3","4","i"}
GeneralKnowledge[2] = {"What is the capital city of the UK?", "London","Belfast","Dublin","Cardiff"}

Can anyone help? This just ends in an error

print(GeneralKnowledge[1][2]):1: attempt to index global 'GeneralKnowledge' (a nil value)

The table is stored within a script in the startergui.

0
Can you include the whole script? What you *have shown* is correct, but since that's not the part that's erroring, it would be good to have that. BlueTaslem 18071 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

I think you forgot table.insert.

Here's how you use table.insert table.insert(TableName,Position,Data) Note: These Parameters are NOT what ROBLOX named them. I just simplified the names. For your table it would be: table.insert(GeneralKnowledge,1,{"What is 2+2?","2","3","4","i"})

GeneralKnowledge is the TableName 1 is the Postiton in the table And you apparently want to put a table in a table. So the second table would be Data

I hope I helped :)

0
What he has is actually correct. You can assign to anywhere in a table to add things to it. The error he's quoting suggests something else entirely is wrong. BlueTaslem 18071 — 9y
Ad

Answer this question