generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Doesn't function as desired.

Previous Thread :: Next Thread 
StealthKing95 is not online. StealthKing95
Joined: 13 Dec 2008
Total Posts: 4263
13 Jan 2013 05:22 PM
The "getNumberType" ALWAYS returns "not a number" no matter is it or not, please help?

Also, this is not roblox Lua, this is standard Lua 5.1.

function getNumberType(n)
   local result
   if type(n) == "number" then
      local str = tostring(n)
      if string.sub(str, 1, 2) == "0." then
         result = "Decimal"
      else
         result = "Integer"
      end
   else
      result = "Not a number"
   end
   return result
end

function wait(t)
   local old = os.time()
   repeat
   until os.time() == old + t
end

while true do
   clear()
   io.write("Type a number: ")
   local num = io.read()
   print(getNumberType(num))
   wait(5)
end
Report Abuse
BlueTaslem is not online. BlueTaslem
Joined: 11 May 2008
Total Posts: 11060
13 Jan 2013 05:43 PM
The type of a string which looks like a number is a string:

print(type("23"))
-->string

So, you need

if type(n) == "string" and tonumber(n) then
n = tonumber(n)
end

at the beginning of the function.
Report Abuse
StealthKing95 is not online. StealthKing95
Joined: 13 Dec 2008
Total Posts: 4263
14 Jan 2013 02:46 AM
I tried doing it in a new way doing also what you said, now any number always returns decimal..

function getNumberType(n)
   local result
   if type(n) == "string" and tonumber(n) then
      n = tostring(n)
      if string.find(n, ".") then
         result = "Decimal"
      else
         result = "Integer"
      end
   else
      result = "Not a number"
   end
   return result
end

function wait(t)
   local old = os.time()
   repeat
   until os.time() == old + t
end

while true do
   clear()
   io.write("Type a number: ")
   local num = io.read()
   print(getNumberType(num))
   wait(5)
end
Report Abuse
helpsyouattime is not online. helpsyouattime
Joined: 24 Jun 2011
Total Posts: 345
14 Jan 2013 02:54 AM
Ill take a look at it,
Report Abuse
StealthKing95 is not online. StealthKing95
Joined: 13 Dec 2008
Total Posts: 4263
14 Jan 2013 09:46 AM
bump
Report Abuse
1Topcop is not online. 1Topcop
Joined: 09 Jun 2009
Total Posts: 6635
14 Jan 2013 11:18 AM
function nType(n)
if(tonumber(n))then
if(tostring(n):match("."))then
return "Double/Float"
else return "Integer"end
else return "Not a number." end end

print(nType("This SHOULD work"))
print(nType(16))
print(nType(10.12))

> Not a number.
> Integer
> Double/Float
Report Abuse
Woodstauk4 is not online. Woodstauk4
Joined: 27 Dec 2010
Total Posts: 3061
14 Jan 2013 11:33 AM
function getNumberType(n)
local result
if type(n) == "string" and tonumber(n) then
n = tostring(n)
if string.find(n, "%.") then
result = "Decimal"
else
result = "Integer"
end
else
result = "Not a number"
end
return result
end

function wait(t)
local old = os.time()
repeat
until os.time() == old + t
end

while true do
clear()
io.write("Type a number: ")
local num = io.read()
print(getNumberType(num))
wait(5)
end


Maybe it was mistaking your search for a string pattern (where '.' is any character), so I made that '%.' (a decimal point)

Probably won't work >_>
Report Abuse
StealthKing95 is not online. StealthKing95
Joined: 13 Dec 2008
Total Posts: 4263
14 Jan 2013 05:35 PM
Thanks woodstauk, it did work! :D
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image