Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Square brackets are giving me a nil value?

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!

3 comments

0

Could you give the actual error?

0

try Q:GetChildren()[box]

0

Questions is a folder, 'Q' is the name of the string values inside the folder. It goes from Q1 to Q20.

2 answers

✓ Accepted answer

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.

4 comments

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.

0

"Questions" is a folder inside the server storage.

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.

0

Thanks, I'll do this.

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

1 comment

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.