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

Table not working? [ANSWERED]

Asked by 9 years ago

Hello scripting helpers.

I just made a script trying to print ONE line from the table and set that one line to the 5 values.

My script explains:-

QuestionValue = game.ServerStorage:WaitForChild("Question") -- Values
Answer1Value = game.ServerStorage:WaitForChild("Answer1") -- Values
Answer2Value = game.ServerStorage:WaitForChild("Answer2") -- Values
Answer3Value = game.ServerStorage:WaitForChild("Answer3") -- Values
QuestionID = game.ServerStorage:WaitForChild("QuestionID") -- Values


Questions = { -- As you see below, the ["1"] is [1], the "Question) ...." is [2] etc etc etc.
["1"] = "Question) What is the name of the dog in Garfield?"; "Odie"; "Fred"; "Steve",
["2"] = "Question) wat is 9+10?"; "21"; "19"; "1",
["3"] = "Question) am i good"; "yes?"; "no!"; "idk"
}

questionpicker = (Questions[math.random(1,#Questions)]) -- Picks a line
print(questionpicker) -- Prints the line
QuestionID = questionpicker[1] -- Sets the value to the line picked which is one.
QuestionValue = questionpicker[2] -- Sets the value to the line picked which is two.
Answer1Value = questionpicker[3] -- Sets the value to the line picked which is three.
Answer2Value = questionpicker[4] -- Sets the value to the line picked which is four.
Answer3Value = questionpicker[5] -- Sets the value to the line picked which is five.

if Questions.table["1"] then -- If the question is 1 then
    print "question 1"
end

If that is not enough explanation, I will show you it by paint:-

http://oi58.tinypic.com/2cfw8k3.jpg

However, the script does not work. What is does is just print one thing like "21" and it does not set the values.

It also errors on line 22 and says:-

ServerScriptService.Script:22: attempt to index field 'table' (a nil value)

I hope you can help me with this script!

Thanks! Nathan.

0
I actually know how to fix this, let me get on my computer. M39a9am3R 3210 — 9y
0
Great! Thanks so much. :] WelpNathan 307 — 9y

1 answer

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

The reason roblox studio is printing 21 is because, you defined it as a value of the table;

Questions = { -- As you see below, the ["1"] is [1], the "Question) ...." is [2] etc etc etc.
["1"] = "Question) What is the name of the dog in Garfield?"; "Odie"; "Fred"; "Steve",
["2"]--[[These are not typically needed, especially in this case.]] = "Question) wat is 9+10?"; "21"--[[21 is defined right here.]]; "19"; "1",
["3"] = "Question) am i good"; "yes?"; "no!"; "idk"
}

But here's the deal with the script I am updating, you can use a table in a table. I am also removing the number that supposedly identifies the question, since ROBLOX already does that for us.

Questions = { -- As you see below, the ["1"] is [1], the "Question) ...." is [2] etc etc etc.
{"Question) What is the name of the dog in Garfield?"; "Odie"; "Fred"; "Steve"};
{"Question) wat is 9+10?"; "21"; "19"; "1"};
{"Question) am i good"; "yes?"; "no!"; "idk"}
}
--So when Questions are called to be printed such as Questions[1] well, it will error at first for trying to print a table value. You can then try printing Questions[1][1] and the script should print "What is the name of the dog in Garfield?" Since you're trying to print the first value of the first table in the table.

At this point, I just found out what those ["1"] and ["2"]'s were meant to do.

I actually got rid of those and had the question id in its separate value in the script. Called qid which will remain consistent and be used as a locator for the question. So whenever qid is called in the table it will go to that table value.

qid = math.random(1,#Questions)
print(Questions[qid]) -- Prints the line
QuestionID = qid --[[Okay, I see what you tried to do there with the ID's earlier, I fixed it. Now it should show the id of the question.]] -- Sets the value to the line picked which is one.
QuestionValue = Questions[qid][1] -- Sets the value to the line picked which is two.
Answer1Value = Questions[qid][2] -- Sets the value to the line picked which is three.
Answer2Value = Questions[qid][3] -- Sets the value to the line picked which is four.
Answer3Value = Questions[qid][4] -- Sets the value to the line picked which is five.

When you had said .table in the script on line 22, you confused the script thinking, hey what do I do? Add a value, remove a value, organize a list? All you had to do was remove that part, and since I messed with the script, I had to remove the ["1"] and try to tamper with is a bit.

if qid==1 then -- If the question is 1 then
    print "question 1"
end

So now, if the qid is equal to 1, the script will print question 1


As for the script printing 21 each time, it's a roblox bug in studio or probably ingame too where the random value will be the first each time, I am not entirely sure how to fix this however.


So now, here's your completed script, if you have any questions, just leave a comment, I have to work today, but hope to get back to you as soon as I can.

QuestionValue = game.ServerStorage:WaitForChild("Question") -- Values
Answer1Value = game.ServerStorage:WaitForChild("Answer1") -- Values
Answer2Value = game.ServerStorage:WaitForChild("Answer2") -- Values
Answer3Value = game.ServerStorage:WaitForChild("Answer3") -- Values
QuestionID = game.ServerStorage:WaitForChild("QuestionID") -- Values


Questions = { -- As you see below, the ["1"] is [1], the "Question) ...." is [2] etc etc etc.
{"Question) What is the name of the dog in Garfield?"; "Odie"; "Fred"; "Steve"};
{"Question) wat is 9+10?"; "21"; "19"; "1"};
{"Question) am i good"; "yes?"; "no!"; "idk"};
}

qid = math.random(1,#Questions)
print(print(qid)) -- Prints the line
QuestionID = qid --[[Okay, I see what you tried to do there with the ID's earlier, I fixed it.]] -- Sets the value to the line picked which is one.
QuestionValue = Questions[qid][1] -- Sets the value to the line picked which is two.
Answer1Value = Questions[qid][2] -- Sets the value to the line picked which is three.
Answer2Value = Questions[qid][3] -- Sets the value to the line picked which is four.
Answer3Value = Questions[qid][4] -- Sets the value to the line picked which is five.

if qid==1 then -- If the question is 1 then
    print "question 1"
end
0
THANK YOU SO MUCH! YOU'RE A STAR. :) Also, on line 16, 17, 18, 19 and 20. I forgot to add a .Value to the end of it. But, it's fixed and I can't thank you enough. WelpNathan 307 — 9y
Ad

Answer this question