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

Checking for Number or String?

I am making a script but I want it to check if the 'Value' entered is a number or a string. I am using

if script.Parent.TextBox.Text - 1 ~= nil then

This works great for a number. But when I enter text I get this:

if script.Parent.Insert.Text - 1 == nil then

'attempt to perform arithmetic on field 'Text' (a string value)' I need a more efficient way to detect whether it is a number or a string. Can anyone help?

1 answer

If a string can be turned into a number, then tonumber will return that number; if not it will return nil.

local number = tonumber(str);

if number then
	print( number * 5 ); -- We can do math on it since it's a number
else
	-- number is nil
	print("You didn't enter a number")
end

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