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

number finder script is not working as intended help please?

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?

1 comment

0

if string cant be converted to number then tonumber returns nil and you cant compare it with a number

1 answer

✓ Accepted answer

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.

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