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: :slight_smile:](https://doy2mn9upadnk.cloudfront.net/images/emoji/twitter/slight_smile.png?v=12)
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](//devforum-uploads.s3.dualstack.us-east-2.amazonaws.com/uploads/original/4X/2/0/6/2067ae5fbd48a2c8bdb2fc7c6b495dca79cf310d.png)
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](//devforum-uploads.s3.dualstack.us-east-2.amazonaws.com/uploads/original/4X/5/f/0/5f01e834a58b1b7189bf45a22394cd52d62381e9.png)
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](//devforum-uploads.s3.dualstack.us-east-2.amazonaws.com/uploads/original/4X/c/c/0/cc01ce3418a37ab8e1c7760e1eece4375e380958.png)
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)```