Calculator Coding issue

I am making a calculator for a computer game and while doing it I had an issue

My output says:

Players.3DRobloxGameDev.PlayerGui.Calculator.Frame.LocalScript:197: attempt to perform arithmetic (sub) on nil and number

My Code:

local MainFrame = script.Parent
local BtnHolder = MainFrame.BtnHolder
local Input = MainFrame.Input
local Output = MainFrame.Output
local Btns = {
[""] = BtnHolder[""],
["+"] = BtnHolder["+"],
["-"] = BtnHolder["-"],
["/"] = BtnHolder["/"],
[β€œ0”] = BtnHolder[β€œ0”],
[β€œ1”] = BtnHolder[β€œ1”],
[β€œ2”] = BtnHolder[β€œ2”],
[β€œ3”] = BtnHolder[β€œ3”],
[β€œ4”] = BtnHolder[β€œ4”],
[β€œ5”] = BtnHolder[β€œ5”],
[β€œ6”] = BtnHolder[β€œ6”],
[β€œ7”] = BtnHolder[β€œ7”],
[β€œ8”] = BtnHolder[β€œ8”],
[β€œ9”] = BtnHolder[β€œ9”],
[β€œC”] = BtnHolder[β€œC”],
["="] = BtnHolder["="]
}
local Side = β€œLeft”
local Operator = β€œβ€
local Num1 = β€œβ€
local Num2 = β€œβ€

MainFrame.Draggable = true
Input.Text = β€œβ€
Output.Text = β€œβ€
Btns[β€œC”].MouseButton1Click:Connect(function()
Input.Text = β€œβ€
Output.Text = β€œβ€
Operator = β€œβ€
Num1 = β€œβ€
Num2 = β€œβ€
Side = β€œLeft”
print(β€œC: Cleared”)
end)
Btns[β€œ0”].MouseButton1Click:Connect(function()
if Side == β€œLeft” then
Input.Text = Input.Text … β€œ0”
Num1 = Num1 … β€œ0”
print(β€œC: Added β€˜0’ to Left”)
elseif Side == β€œRight” then
Input.Text = Input.Text … β€œ0”
Num2 = Num2 … β€œ0”
print(β€œC: Added β€˜0’ to Right”)
end
end)
Btns[β€œ1”].MouseButton1Click:Connect(function()
if Side == β€œLeft” then
Input.Text = Input.Text … β€œ1”
Num1 = Num1 … β€œ1”
print(β€œC: Added β€˜1’ to Left”)
elseif Side == β€œRight” then
Input.Text = Input.Text … β€œ1”
Num2 = Num2 … β€œ1”
print(β€œC: Added β€˜1’ to Right”)
end
end)
Btns[β€œ2”].MouseButton1Click:Connect(function()
if Side == β€œLeft” then
Input.Text = Input.Text … β€œ2”
Num1 = Num1 … β€œ2”
print(β€œC: Added β€˜2’ to Left”)
elseif Side == β€œRight” then
Input.Text = Input.Text … β€œ2”
Num2 = Num2 … β€œ2”
print(β€œC: Added β€˜2’ to Right”)
end
end)
Btns[β€œ3”].MouseButton1Click:Connect(function()
if Side == β€œLeft” then
Input.Text = Input.Text … β€œ3”
Num1 = Num1 … β€œ3”
print(β€œC: Added β€˜3’ to Left”)
elseif Side == β€œRight” then
Input.Text = Input.Text … β€œ3”
Num2 = Num2 … β€œ3”
print(β€œC: Added β€˜3’ to Right”)
end
end)
Btns[β€œ4”].MouseButton1Click:Connect(function()
if Side == β€œLeft” then
Input.Text = Input.Text … β€œ4”
Num1 = Num1 … β€œ4”
print(β€œC: Added β€˜4’ to Left”)
elseif Side == β€œRight” then
Input.Text = Input.Text … β€œ4”
Num2 = Num2 … β€œ4”
print(β€œC: Added β€˜4’ to Right”)
end
end)
Btns[β€œ5”].MouseButton1Click:Connect(function()
if Side == β€œLeft” then
Input.Text = Input.Text … β€œ5”
Num1 = Num1 … β€œ5”
print(β€œC: Added β€˜5’ to Left”)
elseif Side == β€œRight” then
Input.Text = Input.Text … β€œ5”
Num2 = Num2 … β€œ5”
print(β€œC: Added β€˜5’ to Right”)
end
end)
Btns[β€œ6”].MouseButton1Click:Connect(function()
if Side == β€œLeft” then
Input.Text = Input.Text … β€œ6”
Num1 = Num1 … β€œ6”
print(β€œC: Added β€˜6’ to Left”)
elseif Side == β€œRight” then
Input.Text = Input.Text … β€œ6”
Num2 = Num2 … β€œ6”
print(β€œC: Added β€˜6’ to Right”)
end
end)
Btns[β€œ7”].MouseButton1Click:Connect(function()
if Side == β€œLeft” then
Input.Text = Input.Text … β€œ7”
Num1 = Num1 … β€œ7”
print(β€œC: Added β€˜7’ to Left”)
elseif Side == β€œRight” then
Input.Text = Input.Text … β€œ7”
Num2 = Num2 … β€œ7”
print(β€œC: Added β€˜7’ to Right”)
end
end)
Btns[β€œ8”].MouseButton1Click:Connect(function()
if Side == β€œLeft” then
Input.Text = Input.Text … β€œ8”
Num1 = Num1 … β€œ8”
print(β€œC: Added β€˜8’ to Left”)
elseif Side == β€œRight” then
Input.Text = Input.Text … β€œ8”
Num2 = Num2 … β€œ8”
print(β€œC: Added β€˜8’ to Right”)
end
end)
Btns[β€œ9”].MouseButton1Click:Connect(function()
if Side == β€œLeft” then
Input.Text = Input.Text … β€œ9”
Num1 = Num1 … β€œ9”
print(β€œC: Added β€˜9’ to Left”)
elseif Side == β€œRight” then
Input.Text = Input.Text … β€œ9”
Num2 = Num2 … β€œ9”
print(β€œC: Added β€˜9’ to Right”)
end
end)
Btns[""].MouseButton1Click:Connect(function()
if not string.find(Input.Text, "
") and not string.find(Input.Text, β€œ+”) and not string.find(Input.Text, β€œ-”) and not string.find(Input.Text, β€œ/”) then
Input.Text = Input.Text … β€œ"
Operator = "
”
Side = β€œRight”
print(β€œC: Added '’ to Operator")
else
print(β€œC: You can’t do that!”)
end
end)
Btns["+"].MouseButton1Click:Connect(function()
if not string.find(Input.Text, "
”) and not string.find(Input.Text, β€œ+”) and not string.find(Input.Text, β€œ-”) and not string.find(Input.Text, β€œ/”) then
Input.Text = Input.Text … β€œ+”
Operator = β€œ+”
Side = β€œRight”
print(β€œC: Added β€˜+’ to Operator”)
else
print(β€œC: You can’t do that!”)
end
end)
Btns["-"].MouseButton1Click:Connect(function()
if not string.find(Input.Text, β€œ") and not string.find(Input.Text, β€œ+”) and not string.find(Input.Text, β€œ-”) and not string.find(Input.Text, β€œ/”) then
Input.Text = Input.Text … β€œ-”
Operator = β€œ-”
Side = β€œRight”
print(β€œC: Added β€˜-’ to Operator”)
else
print(β€œC: You can’t do that!”)
end
end)
Btns["/"].MouseButton1Click:Connect(function()
if not string.find(Input.Text, "
”) and not string.find(Input.Text, β€œ+”) and not string.find(Input.Text, β€œ-”) and not string.find(Input.Text, β€œ/”) then
Input.Text = Input.Text … β€œ/”
Operator = β€œ/”
Side = β€œRight”
print(β€œC: Added β€˜/’ to Operator”)
else
print(β€œC: You can’t do that!”)
end
end)
Btns["="].MouseButton1Click:Connect(function()
if string.find(Input.Text, β€œ") or string.find(Input.Text, β€œ+”) or string.find(Input.Text, β€œ-”) or string.find(Input.Text, β€œ/”) then
if Operator == "
” then
Output.Text = tostring(tonumber(Num1)*tonumber(Num2))
elseif Operator == β€œ+” then
Output.Text = tostring(tonumber(Num1)+tonumber(Num2))
elseif Operator == β€œ-” then
Output.Text = tostring(tonumber(Num1)-tonumber(Num2))
elseif Operator == β€œ/” then
Output.Text = tostring(tonumber(Num1)/tonumber(Num2))
end
Side = β€œLeft”
Operator = β€œβ€
Num1 = β€œβ€
Num2 = β€œβ€
print(β€œC: Calculated”)
else
print(β€œC: You can’t do that!”)
end
end)

Explorer:

It’s really not clean. Could you please tell which line causes error? 197 is a lot to count for visitor. To this, you can tell something about how does you code work :slight_smile:

Ok line 197 is Output.Text = tostring(tonumber(Num1)-tonumber(Num2)) and the code basically adds up or times or minuses what users put in

Okay, can you put

print(tonumber(Num1))
print(tonumber(Num2))

before line 197?

1 Like

its on line 197 also the ones with Output.Text = tostring(tonumber(Num1)-tonumber(Num2)) are all red underlined

image
Don’t use ”somethinghere”, use β€œsomethinghere”
” is not equal to "
It makes difference

My code is with β€œβ€ I think its the devforum changing it

Can you re-paste your code and put format in your topic like this:
adwadawd
Cause i can’t see your error code clearly, maybe someone else can but I think if you don’t put your code in that format it will look a bit messy

1 Like

You can’t use numerals with quotation marks, 1 = the same as β€œ1” but it’s how you use it.

Ok So Much People Say To Do It Like This

Hello

Or How You Told Me To…
Also I cant figure out how to use the code format like you did.

You just need to put ``` top and the bottom of your script

So if you put that like what i told, your code gonna be like this:

local Variable = "Hello This Is Variable"

Not like this:

local Variable = β€œHello This Is Variable”

This is looks like in a picture:
adwadawdd

local BtnHolder = MainFrame.BtnHolder
local Input = MainFrame.Input
local Output = MainFrame.Output
local Btns = {
	["*"] = BtnHolder["*"],
	["+"] = BtnHolder["+"],
	["-"] = BtnHolder["-"],
	["/"] = BtnHolder["/"],
	["0"] = BtnHolder["0"],
	["1"] = BtnHolder["1"],
	["2"] = BtnHolder["2"],
	["3"] = BtnHolder["3"],
	["4"] = BtnHolder["4"],
	["5"] = BtnHolder["5"],
	["6"] = BtnHolder["6"],
	["7"] = BtnHolder["7"],
	["8"] = BtnHolder["8"],
	["9"] = BtnHolder["9"],
	["C"] = BtnHolder["C"],
	["="] = BtnHolder["="]
}
local Side = "Left"
local Operator = ""
local Num1 = ""
local Num2 = ""

MainFrame.Draggable = true
Input.Text = ""
Output.Text = ""
Btns["C"].MouseButton1Click:Connect(function()
	Input.Text = ""
	Output.Text = ""
	Operator = ""
	Num1 = ""
	Num2 = ""
	Side = "Left"
	print("C: Cleared")
end)
Btns["0"].MouseButton1Click:Connect(function()
	if Side == "Left" then
		Input.Text = Input.Text .. "0"
		Num1 = Num1 .. "0"
		print("C: Added '0' to Left")
	elseif Side == "Right" then
		Input.Text = Input.Text .. "0"
		Num2 = Num2 .. "0"
		print("C: Added '0' to Right")
	end
end)
Btns["1"].MouseButton1Click:Connect(function()
	if Side == "Left" then
		Input.Text = Input.Text .. "1"
		Num1 = Num1 .. "1"
		print("C: Added '1' to Left")
	elseif Side == "Right" then
		Input.Text = Input.Text .. "1"
		Num2 = Num2 .. "1"
		print("C: Added '1' to Right")
	end
end)
Btns["2"].MouseButton1Click:Connect(function()
	if Side == "Left" then
		Input.Text = Input.Text .. "2"
		Num1 = Num1 .. "2"
		print("C: Added '2' to Left")
	elseif Side == "Right" then
		Input.Text = Input.Text .. "2"
		Num2 = Num2 .. "2"
		print("C: Added '2' to Right")
	end
end)
Btns["3"].MouseButton1Click:Connect(function()
	if Side == "Left" then
		Input.Text = Input.Text .. "3"
		Num1 = Num1 .. "3"
		print("C: Added '3' to Left")
	elseif Side == "Right" then
		Input.Text = Input.Text .. "3"
		Num2 = Num2 .. "3"
		print("C: Added '3' to Right")
	end
end)
Btns["4"].MouseButton1Click:Connect(function()
	if Side == "Left" then
		Input.Text = Input.Text .. "4"
		Num1 = Num1 .. "4"
		print("C: Added '4' to Left")
	elseif Side == "Right" then
		Input.Text = Input.Text .. "4"
		Num2 = Num2 .. "4"
		print("C: Added '4' to Right")
	end
end)
Btns["5"].MouseButton1Click:Connect(function()
	if Side == "Left" then
		Input.Text = Input.Text .. "5"
		Num1 = Num1 .. "5"
		print("C: Added '5' to Left")
	elseif Side == "Right" then
		Input.Text = Input.Text .. "5"
		Num2 = Num2 .. "5"
		print("C: Added '5' to Right")
	end
end)
Btns["6"].MouseButton1Click:Connect(function()
	if Side == "Left" then
		Input.Text = Input.Text .. "6"
		Num1 = Num1 .. "6"
		print("C: Added '6' to Left")
	elseif Side == "Right" then
		Input.Text = Input.Text .. "6"
		Num2 = Num2 .. "6"
		print("C: Added '6' to Right")
	end
end)
Btns["7"].MouseButton1Click:Connect(function()
	if Side == "Left" then
		Input.Text = Input.Text .. "7"
		Num1 = Num1 .. "7"
		print("C: Added '7' to Left")
	elseif Side == "Right" then
		Input.Text = Input.Text .. "7"
		Num2 = Num2 .. "7"
		print("C: Added '7' to Right")
	end
end)
Btns["8"].MouseButton1Click:Connect(function()
	if Side == "Left" then
		Input.Text = Input.Text .. "8"
		Num1 = Num1 .. "8"
		print("C: Added '8' to Left")
	elseif Side == "Right" then
		Input.Text = Input.Text .. "8"
		Num2 = Num2 .. "8"
		print("C: Added '8' to Right")
	end
end)
Btns["9"].MouseButton1Click:Connect(function()
	if Side == "Left" then
		Input.Text = Input.Text .. "9"
		Num1 = Num1 .. "9"
		print("C: Added '9' to Left")
	elseif Side == "Right" then
		Input.Text = Input.Text .. "9"
		Num2 = Num2 .. "9"
		print("C: Added '9' to Right")
	end
end)
Btns["*"].MouseButton1Click:Connect(function()
	if not string.find(Input.Text, "*") and not string.find(Input.Text, "+") and not string.find(Input.Text, "-") and not string.find(Input.Text, "/") then
		Input.Text = Input.Text .. "*"
		Operator = "*"
		Side = "Right"
		print("C: Added '*' to Operator")
	else
		print("C: You can't do that!")
	end
end)
Btns["+"].MouseButton1Click:Connect(function()
	if not string.find(Input.Text, "*") and not string.find(Input.Text, "+") and not string.find(Input.Text, "-") and not string.find(Input.Text, "/") then
		Input.Text = Input.Text .. "+"
		Operator = "+"
		Side = "Right"
		print("C: Added '+' to Operator")
	else
		print("C: You can't do that!")
	end
end)
Btns["-"].MouseButton1Click:Connect(function()
	if not string.find(Input.Text, "*") and not string.find(Input.Text, "+") and not string.find(Input.Text, "-") and not string.find(Input.Text, "/") then
		Input.Text = Input.Text .. "-"
		Operator = "-"
		Side = "Right"
		print("C: Added '-' to Operator")
	else
		print("C: You can't do that!")
	end
end)
Btns["/"].MouseButton1Click:Connect(function()
	if not string.find(Input.Text, "*") and not string.find(Input.Text, "+") and not string.find(Input.Text, "-") and not string.find(Input.Text, "/") then
		Input.Text = Input.Text .. "/"
		Operator = "/"
		Side = "Right"
		print("C: Added '/' to Operator")
	else
		print("C: You can't do that!")
	end
end)
Btns["="].MouseButton1Click:Connect(function()
	if string.find(Input.Text, "*") or string.find(Input.Text, "+") or string.find(Input.Text, "-") or string.find(Input.Text, "/") then
		if Operator == "*" then
			Output.Text = tostring(tonumber(Num1)*tonumber(Num2))
		elseif Operator == "+" then
			Output.Text = tostring(tonumber(Num1)+tonumber(Num2))
		elseif Operator == "-" then
			Output.Text = tostring(tonumber(Num1)-tonumber(Num2))
		elseif Operator == "/" then
			Output.Text = tostring(tonumber(Num1)/tonumber(Num2))
		end
		Side = "Left"
		Operator = ""
		Num1 = ""
		Num2 = ""
		print("C: Calculated")
	else
		print("C: You can't do that!")
	end
end)```