Calculator Help

Good morning,

I just need some help with the calculator I have been working on. I have an issue when it comes to pulling the two numbers out of the textboxes and execute the calculation. If I give the textbox(s) the value β€˜5’ before play-testing the game, the calculator gives me all of the right results. If I try to type something into the textbox after clearing it, it gives me the same answer as before not the new numbers. I believe this is an error because if I leave the textboxes blank I get an arithmetic error on a nil value.

My code for the addition button is as follows:

script.Parent.MouseButton1Down:connect(function()
local locTxt1 = game.StarterGui.TextBoxes.sgNumbers.txtNumber1.Text
local locTxt2 = game.StarterGui.TextBoxes.sgNumbers.txtNumber2.Text
local locAAnswer = 0 + tonumber(locTxt1) + tonumber(locTxt2)
script.Parent.Parent.Parent.Parent.Labels.sgAnswerLabel.lblAnswer.Text = locAAnswer
end)

Thanks for any help.

1 Like

You are getting the numbers from the StarterGui copy, you want to instead use the numbers from the textboxes in your PlayerGui.

Like so:

local LocalPlayer = game.Players.LocalPlayer

script.Parent.MouseButton1Down:connect(function()
local locTxt1 = LocalPlayer.PlayerGui.TextBoxes.sgNumbers.txtNumber1.Text
local locTxt2 = LocalPlayer.PlayerGui.TextBoxes.sgNumbers.txtNumber2.Text
local locAAnswer = 0 + tonumber(locTxt1) + tonumber(locTxt2)
script.Parent.Parent.Parent.Parent.Labels.sgAnswerLabel.lblAnswer.Text = locAAnswer
end)
3 Likes

Yeah that fixed the problem, thanks for all the help!

TheGamer101 has fixed your issue, but your code can still be improved by using variables and indenting.

Here is the clean version of the working version:

local LocalPlayer = game:GetService("Players").LocalPlayer
local Button = script.Parent
local sgNumbers = LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("TextBoxes"):WaitForChild("sgNumbers")

Button.MouseButton1Down:connect(function()
    local locTxt1 = tonumber(sgNumbers.txtNumber1.Text)
    local locTxt2 = tonumber(sgNumbers.txtNumber2.Text)
    local locAAnswer = 0 + locTxt1 + locTxt2
    script.Parent.Parent.Parent.Parent.Labels.sgAnswerLabel.lblAnswer.Text = locAAnswer -- I recommend you start indexing from StarterGui
end)
1 Like

Okay, thanks!

Fairly sure the indention issue isn’t intentional. I can’t say for certain though.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.