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
 

Calculator Help

Previous Thread :: Next Thread 
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 03:56 PM
This is a script for my calculator gui. It works, but I'm trying to get it to detect if the string/variable or whatever is completely numbers and without a letter. I tried using tonumber() ~= nil/false but that doesn't work. What should I add?

function Calculate()
local t = script.Parent
local c = t.Num1.Text
local g = t.Num2.Text
local cal = t.Answer
local add = c + g
local sub = c - g
local mul = c * g
local div = c / g
local var = t.Variable.Text
if var == "+" and tonumber(c) ~= false and tonumber(g) ~= false then
cal.Text = "Answer is "..tostring(add)
elseif var == "-" and tonumber(c) ~= false and tonumber(g) ~= false then
cal.Text = "Answer is "..tostring(sub)
elseif var == "*" and tonumber(c) ~= false and tonumber(g) ~= false then
cal.Text = "Answer is"..tostring(mul)
elseif var == "x" and tonumber(c) ~= false and tonumber(g) ~= false then
cal.Text = "Answer is "..tostring(mul)
elseif var == "/" and tonumber(c) ~= false and tonumber(g) ~= false then
cal.Text = "Answer is "..tostring(div)
else
cal.Text = "Invalid Equation"
end
end

local e = script.Parent
e.Num1.Changed:connect(Calculate)
e.Num2.Changed:connect(Calculate)
e.Variable.Changed:connect(Calculate)
e.Answer.Changed:connect(Calculate)


while true do
wait(0.1)
local t = script.Parent
if t.Answer.Text == "nil" then
t.Answer.Text = "Invalid Equation"
end
end
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 04:45 PM
So how do I detect that?
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
22 Mar 2013 04:47 PM
tonumber(X) ~= nil
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 04:53 PM
I changed false to nil and it still attempted to do arithmetic
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
22 Mar 2013 04:55 PM
You need to check before you do the calculations.
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 05:05 PM
In the script, it's supposed to check if it is.

This:

if var == "+" and tonumber(c) ~= nil and tonumber(g) ~= nil then
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
22 Mar 2013 05:09 PM

function Calculate()
local t = script.Parent
local c = t.Num1.Text
local g = t.Num2.Text
local cal = t.Answer
local add = c + g
local sub = c - g
local mul = c * g
local div = c / g


Check before the calculations.
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 05:12 PM
Like this?


local add = tonumber(c) + tonumber(g)

if add ~= nil then
Report Abuse
As8D is not online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
22 Mar 2013 05:33 PM
function Calculate()
local t = script.Parent
local c = t.Num1.Text
local g = t.Num2.Text
local cal = t.Answer

if not (tostring(c) and tostring(g)) then
cal.Text = "Invalid arguments"
return
end

local add = c + g
local sub = c - g
local mul = c * g
local div = c / g
--stoof.


- As, LIKE THAT! Oh yesh, that's ... how I now leave my computer.
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 05:49 PM
So like this?

function Calculate()
local t = script.Parent
local c = t.Num1.Text
local g = t.Num2.Text
local cal = t.Answer
local add = c + g
local sub = c - g
local mul = c * g
local div = c / g
local var = t.Variable.Text
if not (tostring(c) and tostring(g)) then
cal.Text = "Invalid arguments"
elseif var == "+" then
cal.Text = "Answer is "..tostring(add)
elseif var == "-" then
cal.Text = "Answer is "..tostring(sub)
elseif var == "*" then
cal.Text = "Answer is"..tostring(mul)
elseif var == "x" then
cal.Text = "Answer is "..tostring(mul)
elseif var == "/" then
cal.Text = "Answer is "..tostring(div)
else
cal.Text = "Invalid Equation"
end
end

local e = script.Parent
e.Num1.Changed:connect(Calculate)
e.Num2.Changed:connect(Calculate)
e.Variable.Changed:connect(Calculate)
e.Answer.Changed:connect(Calculate)


while true do
wait(0.1)
local t = script.Parent
if t.Answer.Text == "nil" then
t.Answer.Text = "Invalid Equation"
end
end
Report Abuse
As8D is not online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
22 Mar 2013 05:58 PM
Umgarish.

Listen:


If we say that you inserted "Hello" and "2" into your calculator GUI, then:

local c = "Hello"
local g = "2"
local cal = t.Answer

local add = "Hello" + "2" <-- Error.
local sub = "Hello" - "2" <-- Error.
local mul = "Hello" * "2" <-- Error.
local div = "Hello" / "2" <-- Error.

-- stuff.


Can you see what I mean? First of all, you cannot use math on strings. Also, if you simply put "tonumber" on c & g, then ex. add would be: add = nil + 2 <-- Error!!!!


See, you need to:
- Convert c & g variables into numbers.
- Check that c & g actually ARE numbers!
- And don't use math before doing this.
- As, number four. I win.
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 06:01 PM
Lol I forgot to mention that my question was how to check if it WAS a number. Stupid me.
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
22 Mar 2013 06:02 PM
If you try to do math on a String Roblox converts it to a number automatically.
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
22 Mar 2013 06:04 PM
you could just use pcall()

local s,Error = pcall(funtion()
--Calculator stuff
end)
if Error then
Thing.Text = "Error"
end
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
22 Mar 2013 06:06 PM
There's also the type function...

if type(valuehere) == "number"

That's the most obvious way.

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 06:08 PM
I dunno wut pcall is.
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
22 Mar 2013 06:09 PM
"There's also the type function...

if type(valuehere) == "number"

That's the most obvious way."

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 06:13 PM
lolok
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 06:15 PM
I must now remember that I'm still a slight idiot to Lua.
Report Abuse
As8D is not online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
22 Mar 2013 06:17 PM
^notsopwn'd; Not working on hex-decimals though D: And base-32...

Sure Blaze, but it won't work if we have "2", only 2. I wish they expanded that function to find CFrames, Vector3s, BrickColors, ray, region3, color3 and all that stuff.


- As, we have visited any wiki.roblox.com page you can access as visitor... since idk.
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
22 Mar 2013 06:17 PM
So like dis?

if var == "+" and type(g) == "number" and type(c) == "number" then
Report Abuse
As8D is not online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
22 Mar 2013 06:18 PM
Yeah, depending on whatever you want, you can either put the whole function inside an if case, or simply stop the function if something is wrong.



- As, postmatrix!
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
22 Mar 2013 06:18 PM
Oh, As is right. Do:

if not not tonumber(valueHere) then

Remember to use not twice.

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
As8D is not online. As8D
Joined: 24 Dec 2009
Total Posts: 2907
22 Mar 2013 06:28 PM
not not... will equalize each other! D:

not true == false
not not true == true!!!

look, what I did was:
if not (tonumber(c) and tonumber(g)) then

Which is equal to this case:
if not tonumber(c) or not tonumber(g) then


because:
true & true --> true
true & false --> false
false & true --> false
false & false --> false

Meaning that if just a single (or both) of the two variables are nil/false, the result will be false. Then we invert it, so it get's true. And of course, inverting true (My term of inverting is 'not'), the result will be false, of which we then skip the case.



This is basic knowledge about operators. Learn it. Forever.
- As, diodes, light, power, generator, sword, noob...
Report Abuse
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
22 Mar 2013 06:34 PM
@As - No. Using the not operator twice on the tonumber function will return a boolean. If the argument you use is a number--in either number or string form--then it will return true. Otherwise it will return false.

Example:

local value = "5"
print(not not tonumber(value))

> true

local value = "hi"
print(not not tonumber(value))

> false

ElectricBlaze • Programmer • Wiki Writer | http://wiki.roblox.com/index.php/User:ElectricBlaze
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 2Go to page: [1], 2 Next
 
 
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