Arithmetic (Math) error

Hello,

I am trying to make a little computer unit on a project and when I try to make a simple adding mechanism I am getting this error:

attempt to perform arithmetic (add) on nil

Here is my code:

--// OPERATIONS
local Add = script.Parent.Operations.Add
local Subtract = script.Parent.Operations.Sub
local Divide = script.Parent.Operations.Div
local Multiply = script.Parent.Operations.Mult
local ChosenOperation = nil

Add.Activated:Connect(function()
	ChosenOperation = "Add"
	Add.ImageColor3 = Color3.fromRGB(68, 112, 177)
	Subtract.ImageColor3 = Color3.fromRGB(85, 139, 220)
	Divide.ImageColor3 = Color3.fromRGB(85, 139, 220)
	Multiply.ImageColor3 = Color3.fromRGB(85, 139, 220)
end)

Subtract.Activated:Connect(function()
	ChosenOperation = "Subtract"
	Add.ImageColor3 = Color3.fromRGB(85, 139, 220)
	Subtract.ImageColor3 = Color3.fromRGB(68, 112, 177)
	Divide.ImageColor3 = Color3.fromRGB(85, 139, 220)
	Multiply.ImageColor3 = Color3.fromRGB(85, 139, 220)
end)

Divide.Activated:Connect(function()
	ChosenOperation = "Divide"
	Add.ImageColor3 = Color3.fromRGB(85, 139, 220)
	Subtract.ImageColor3 = Color3.fromRGB(85, 139, 220)
	Divide.ImageColor3 = Color3.fromRGB(68, 112, 177)
	Multiply.ImageColor3 = Color3.fromRGB(85, 139, 220)
end)

Multiply.Activated:Connect(function()
	ChosenOperation = "Multiply"
	Add.ImageColor3 = Color3.fromRGB(85, 139, 220)
	Subtract.ImageColor3 = Color3.fromRGB(85, 139, 220)
	Divide.ImageColor3 = Color3.fromRGB(85, 139, 220)
	Multiply.ImageColor3 = Color3.fromRGB(68, 112, 177)
end)

--// INPUTS
local NumberOne = tonumber(script.Parent.Inputs.One.TextLabel.Text)
local NumberTwo = tonumber(script.Parent.Inputs.Two.TextLabel.Text)
local Go = script.Parent.Inputs.Go

--// STEP1
local Step1 = script.Parent.Step1
local Step1Subtitle = script.Parent.Step1Subtitle

--// VARIABLES
local Equation = nil
local Answer = nil
local HidePage = script.Parent.HiddenPage
local LoadingScripts = HidePage.LoadingScripts
local Error = script.Parent.Error

--// ADD PREVIOUS
function AddPrevious()
	print(Equation)
	print(Answer)
	local PreviousAnswer = script.Parent.PreviousAnswers.PreviousAnswer:Clone()
	PreviousAnswer.Equation.Text = Equation
	PreviousAnswer.Answer.Text = Answer
	PreviousAnswer.Visible = true
	PreviousAnswer.Parent = script.Parent.PreviousAnswers
end

--// CALCULATE
function Calculate()
	if ChosenOperation == "Add" then
		local FoundAnswer = NumberOne + NumberTwo-- THIS IS THE LINE I AM GETTING THE ERROR
		Equation = NumberOne.." + "..NumberTwo
		Answer = FoundAnswer
		print(FoundAnswer)
	elseif ChosenOperation == "Subtract" then
		
	elseif ChosenOperation == "Multiply" then
		
	elseif ChosenOperation == "Divide" then
		
	else
		Error.Text = "Error: Unknown Operation."
	end
	wait(0.5)
	AddPrevious()
end

--// GO
Go.Activated:Connect(function()
	wait(0.5)
	Calculate()
end)


Does anyone know what I am doing wrong? Or how to fix it?

Thanks.

1 Like

What are you inputting for both of the textboxes

I use the number 2 in both inputs.

The variables NumberOne and NumberTwo are static, aka they never change because you are only referencing the text once. When you do the calculation you need to convert the text to number, not before.

Ok I tried doing it this way:


--// INPUTS
local NumberOne = script.Parent.Inputs.One.TextLabel.Text
local NumberTwo = script.Parent.Inputs.Two.TextLabel.Text

--// CALCULATE
function Calculate()
	if ChosenOperation == "Add" then
		local FoundAnswer = tonumber(NumberOne) + tonumber(NumberTwo)
		Equation = tonumber(NumberOne).." + "..tonumber(NumberTwo)
		Answer = FoundAnswer
		print(FoundAnswer)
	elseif ChosenOperation == "Subtract" then
		
	elseif ChosenOperation == "Multiply" then
		
	elseif ChosenOperation == "Divide" then
		
	else
		Error.Text = "Error: Unknown Operation."
	end
	wait(0.5)
	AddPrevious()
end


Everything else is still in there but just to help you see better.
I got the same error, is this is what you were talking about?

He was talking about moving

local NumberOne = script.Parent.Inputs.One.TextLabel.Text
local NumberTwo = script.Parent.Inputs.Two.TextLabel.Text

Under

function Calculate()

So it would look like this

function Calculate()
    local NumberOne = script.Parent.Inputs.One.TextLabel.Text
    local NumberTwo = script.Parent.Inputs.Two.TextLabel.Text

Edit for clarity: Values of variables are only defined when you set them so having your 2 numbers* outside the function means they will never change away from nil once you place them under the function the will update every time the function runs