Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Calculator Gui Answer value not popping up?

Asked by 5 years ago

I made a calculator Gui simply out of boredom, but for some reason the Answer value doesn't pop up. The printing part works, to say the least.

01script.Parent.MouseButton1Down:connect(function()
02    local Method = script.Parent.Parent.Method.Text
03    local Num1 = script.Parent.Parent.Num1.Text
04    local Num2 = script.Parent.Parent.Num2.Text
05    local Answer = script.Parent.Parent.Answer.Text
06    if Method == "+" then
07        Answer = Num1+Num2
08    elseif Method == "-" then
09        Answer = Num1-Num2
10    elseif Method == "*" then
11        Answer = Num1*Num2
12    elseif Method == "/" then
13        Answer = Num1/Num2
14    elseif Method == "%" then
15        local Percentage = (10^-2) * Num1
View all 28 lines...

1 answer

Log in to vote
0
Answered by
Wiscript 622 Moderation Voter
5 years ago

When you said on line 8: local Answer = script.Parent.Parent.Answer.Text You made the variable "Answer" whatever was in that TextLabel/TextBox's text. So if you were to print "Answer" straight after that, you would get whatever was written there at the time. Therefore when you later on wrote Answer = <method> All that did was redefine the variable Answer to whatever the arithmetic equation came back as, you didn't tell it to change the text.

To fix this, it's very simple. Simply tell it to actually change the text, rather than store it in the variable Answer, which is what you were doing. Furthermore, since you're changing a text to a number, it's a good idea to inform the script. Use the method tostring() to do this, as seen below.

01script.Parent.MouseButton1Down:connect(function()
02    local Method = script.Parent.Parent.Method.Text
03    local Num1 = script.Parent.Parent.Num1.Text
04    local Num2 = script.Parent.Parent.Num2.Text
05    local Answer = script.Parent.Parent.Answer -- Define the variable as the textlabel
06    if Method == "+" then
07        Answer.Text = tostring(Num1+Num2) -- Edit the text to show it
08    elseif Method == "-" then
09        Answer.Text = tostring(Num1-Num2) -- Use tostring to convert a number/integer to a normal text
10    elseif Method == "*" then
11        Answer.Text = tostring(Num1*Num2)
12    elseif Method == "/" then
13        Answer.Text = tostring(Num1/Num2)
14    elseif Method == "%" then
15        local Percentage = (10^-2) * Num1
View all 28 lines...
0
I tested out the improved script you made and it worked. Thanks a bunch! RedWirePlatinum 53 — 5y
Ad

Answer this question