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

01script.Parent.BackGround2.Enter.MouseButton1Click:connect( function()
02        local text = script.Parent.BackGround2.OrderNumberHandler.Text
03        local number = tonumber(text)
04        print("Number ", number)
05        local OrdersHandler = script.Parent.BackGround3
06 if number >= 6 or not number then
07     script.Parent.BackGround2.Enter.Text = "Number Not found"
08end
09 
10 
11        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:

01script.Parent.BackGround2.Enter.MouseButton1Click:connect( function()
02        local text = script.Parent.BackGround2.OrderNumberHandler.Text
03        local number = tonumber(text)
04 
05    if number ~= nil then
06 
07            print("Number ", number)
08            local OrdersHandler = script.Parent.BackGround3
09 
10        if number >= 6 or not number then
11                 script.Parent.BackGround2.Enter.Text = "Number Not found"
12        end
13    end
14end)

Why your script doesn't work:

01script.Parent.BackGround2.Enter.MouseButton1Click:connect( function()
02        local text = script.Parent.BackGround2.OrderNumberHandler.Text
03        local number = tonumber(text)
04        print("Number ", number)
05        local OrdersHandler = script.Parent.BackGround3
06 if number >= 6 or not number then
07     script.Parent.BackGround2.Enter.Text = "Number Not found"
08end
09 
10 
11        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