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

Printed code from array not working?

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago

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)

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. Ziffixture 6913 — 3y
0
The actual issue lies within the while loop, it's yielding the thread....... Ziffixture 6913 — 3y
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. Ziffixture 6913 — 3y
Ad

Answer this question