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

Square brackets are giving me a nil value?

Asked by 3 years ago

I am trying to get my script to find a random value within another folder but on like 16 where I use square brackets, it says that I attempted to index nil ([box]) with 'Q'.

local numbersentered = {}

script.Parent.BackgroundFrame.QuestionNumber.Changed:Connect(function()

    local box = math.random(1,20)
    print(box)
    table.insert(numbersentered, box)

    for i,v in pairs(numbersentered)do
        if box == v then
            local box = math.random(1,20)
            print(box)
            table.insert(numbersentered, box)

        else
            local text = game.ServerStorage:FindFirstChild("Questions").Q[box].Value
            for  i = 1, #text do
                script.Parent.TextLabel.Text = string.sub (text, 1, i)
            end

        end
    end

end)

Any help is wonderful!

0
Could you give the actual error? Roger111 347 — 3y
0
try Q:GetChildren()[box] greatneil80 2647 — 3y
0
Questions is a folder, 'Q' is the name of the string values inside the folder. It goes from Q1 to Q20. GoingToAnnoyYou 2 — 3y

2 answers

Log in to vote
0
Answered by
Roger111 347 Moderation Voter
3 years ago

ANSWER: So according to your error, it doesn't seem to have to do with box. Your error (translated) means that :FindFirstChild("Questions") is nil.

Note: I can't help you further until I see your hierarchy and what this Questions object is that you are looking for.

0
Questions is a folder which has a load of string values in it called "Q1" for example. It goes from Q1 to Q20. I am trying to get their values by calculating a random value, concatenating them and then bringing back the value of the string value into the script. GoingToAnnoyYou 2 — 3y
0
"Questions" is a folder inside the server storage. GoingToAnnoyYou 2 — 3y
0
Separate your references out. Instead of make a long line, reference each thing in a variable, and then when you get to `Q` you will see what the real problem is. Roger111 347 — 3y
0
Thanks, I'll do this. GoingToAnnoyYou 2 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The full error is:

05:04:51.791 - Players.GoingToAnnoyYou.PlayerGui.ApplicationUI.QuestionScript:16: attempt to index nil with 'Q'
05:04:51.791 - Stack Begin
05:04:51.791 - Script 'Players.GoingToAnnoyYou.PlayerGui.ApplicationUI.QuestionScript', Line 16
05:04:51.792 - Stack End
0
Thanks for accepting my answer! Glad I could help! Also next time, you can just edit your question and add the stuff that someone asks for. And notify them via comment on their post. cheers! Roger111 347 — 3y

Answer this question