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

number finder script is not working as intended help please?

Asked by
raid6n 2196 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

here' the script

script.Parent.BackGround2.Enter.MouseButton1Click:connect( function()
        local text = script.Parent.BackGround2.OrderNumberHandler.Text
        local number = tonumber(text)
        print("Number ", number)
        local OrdersHandler = script.Parent.BackGround3
 if number >= 6 or not number then
     script.Parent.BackGround2.Enter.Text = "Number Not found"
end


        end)

output when i put a letter: 08:59:09.714 - Players.raid6n.PlayerGui.ScreenGui.Main Script:6: attempt to compare number with nil

help?

0
if string cant be converted to number then tonumber returns nil and you cant compare it with a number Lazarix9 245 — 5y

1 answer

Log in to vote
1
Answered by
Lazarix9 245 Moderation Voter
5 years ago

I didn't test this but let's hope it works:

script.Parent.BackGround2.Enter.MouseButton1Click:connect( function()
        local text = script.Parent.BackGround2.OrderNumberHandler.Text
        local number = tonumber(text)

    if number ~= nil then

            print("Number ", number)
            local OrdersHandler = script.Parent.BackGround3

        if number >= 6 or not number then
                 script.Parent.BackGround2.Enter.Text = "Number Not found"
        end
    end
end)

Why your script doesn't work:

script.Parent.BackGround2.Enter.MouseButton1Click:connect( function()
        local text = script.Parent.BackGround2.OrderNumberHandler.Text
        local number = tonumber(text)
        print("Number ", number)
        local OrdersHandler = script.Parent.BackGround3
 if number >= 6 or not number then
     script.Parent.BackGround2.Enter.Text = "Number Not found"
end


        end)

If a string can't be converted into a number, then tonumber() returns nil. If you put something that isn't a number into text then an error occurs at line 6, saying that it can't compare nil to a number value. This is easily fixed by adding an if statement to check if number is equal to nil.

Ad

Answer this question