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

Printed code from array not working?

So what I'm trying to do is, there is a randomly generated code that is put into the "array" table, and it prints the first item from that table. And when I use the printed code from the output, and put it into the textbox, and click the button, it doesn't print anything. I'm using tonumber because any number put into a textbox is a string, please help.

local array = {}
local textBox = script.Parent.Parent.TextBox

while true do
	local randomValue = math.random(100,9999)
	table.insert(array, randomValue)
	wait(1)
	print(array[1])
end

script.Parent.MouseButton1Click:Connect(function()
	if textBox.Text == table.find(array, tonumber(textBox.Text)) then
		print("Code successful")
	else
		print("Code unsuccessful")
	end
end)

1 answer

You said "if textBox.Text == table.find(array, tonumber(textBox.Text)) then print("Code Successful"). This means that the print will only occur IF the textBox.Text comes back true. However you are never changing this value which means that textBox.Text is nil. To change this just simply do this

	local array = {}
	local textBox = script.Parent.Parent.TextBox
	 
	
	while true do
	    local randomValue = math.random(100,9999)
	    table.insert(array, randomValue)
	    wait(1)
	    print(array[1])
	end
	 
	textBox.Text = (array[1]) -- Switch this code to what you like :)
	script.Parent.MouseButton1Click:Connect(function()
	    if textBox.Text == table.find(array, tonumber(textBox.Text)) then
	        print("Code successful")
	    else
	        print("Code unsuccessful")
	    end
	end)

3 comments

0

If you read the code, you'd realize that they're inheriting the text property (which is dynamic, you should know TextBox's are used to write text into...) upon the click of a supposed confirmation TextButton.

0

The actual issue lies within the while loop, it's yielding the thread.......

0

@ack_superbear, you can resolve this quickly by spawning the loop in a separate thread with spawn(), research this on the Developer Wiki to learn how to apply it.

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