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 4 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.

script.Parent.MouseButton1Down:connect(function()
    local Method = script.Parent.Parent.Method.Text
    local Num1 = script.Parent.Parent.Num1.Text
    local Num2 = script.Parent.Parent.Num2.Text
    local Answer = script.Parent.Parent.Answer.Text
    if Method == "+" then
        Answer = Num1+Num2
    elseif Method == "-" then
        Answer = Num1-Num2
    elseif Method == "*" then
        Answer = Num1*Num2
    elseif Method == "/" then
        Answer = Num1/Num2
    elseif Method == "%" then
        local Percentage = (10^-2) * Num1
        Answer = Percentage * Num2
    elseif Method == "<" then
        Answer = Num1<Num2
    elseif Method == ">" then
        Answer = Num1>Num2
    elseif Method == "=" then
        Answer = Num1==Num2
    elseif Method == "^" then
        Answer = Num1^Num2
    else
        print("The input method is unsupported")
    end
end)

1 answer

Log in to vote
0
Answered by
Wiscript 622 Moderation Voter
4 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.

script.Parent.MouseButton1Down:connect(function()
    local Method = script.Parent.Parent.Method.Text
    local Num1 = script.Parent.Parent.Num1.Text
    local Num2 = script.Parent.Parent.Num2.Text
    local Answer = script.Parent.Parent.Answer -- Define the variable as the textlabel 
    if Method == "+" then
        Answer.Text = tostring(Num1+Num2) -- Edit the text to show it
    elseif Method == "-" then
        Answer.Text = tostring(Num1-Num2) -- Use tostring to convert a number/integer to a normal text
    elseif Method == "*" then
        Answer.Text = tostring(Num1*Num2)
    elseif Method == "/" then
        Answer.Text = tostring(Num1/Num2)
    elseif Method == "%" then
        local Percentage = (10^-2) * Num1
        Answer.Text = tostring(Percentage * Num2)
    elseif Method == "<" then
        Answer.Text = tostring(Num1<Num2)
    elseif Method == ">" then
        Answer.Text = tostring(Num1>Num2)
    elseif Method == "=" then
        Answer.Text = tostring(Num1==Num2)
    elseif Method == "^" then
        Answer.Text = tostring(Num1^Num2)
    else
        print("The input method is unsupported")
    end
end)
0
I tested out the improved script you made and it worked. Thanks a bunch! RedWirePlatinum 53 — 4y
Ad

Answer this question