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

Calculator Gui Answer value not popping up?

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

✓ Accepted answer

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)

1 comment

0

I tested out the improved script you made and it worked. Thanks a bunch!

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