Existence_YT

[RobloxStudioProjects] Making a Calculator - (1) LocalScript

Mar 9th, 2019
1,508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.81 KB | None | 0 0
  1. local MainFrame = script.Parent
  2. local BtnHolder = MainFrame.BtnHolder
  3. local Input = MainFrame.Input
  4. local Output = MainFrame.Output
  5. local Btns = {
  6.     ["*"] = BtnHolder["*"],
  7.     ["+"] = BtnHolder["+"],
  8.     ["-"] = BtnHolder["-"],
  9.     ["/"] = BtnHolder["/"],
  10.     ["0"] = BtnHolder["0"],
  11.     ["1"] = BtnHolder["1"],
  12.     ["2"] = BtnHolder["2"],
  13.     ["3"] = BtnHolder["3"],
  14.     ["4"] = BtnHolder["4"],
  15.     ["5"] = BtnHolder["5"],
  16.     ["6"] = BtnHolder["6"],
  17.     ["7"] = BtnHolder["7"],
  18.     ["8"] = BtnHolder["8"],
  19.     ["9"] = BtnHolder["9"],
  20.     ["C"] = BtnHolder["C"],
  21.     ["="] = BtnHolder["="]
  22. }
  23. local Side = "Left"
  24. local Operator = ""
  25. local Num1 = ""
  26. local Num2 = ""
  27.  
  28. MainFrame.Draggable = true
  29. Input.Text = ""
  30. Output.Text = ""
  31. Btns["C"].MouseButton1Click:Connect(function()
  32.     Input.Text = ""
  33.     Output.Text = ""
  34.     Operator = ""
  35.     Num1 = ""
  36.     Num2 = ""
  37.     Side = "Left"
  38.     print("C: Cleared")
  39. end)
  40. Btns["0"].MouseButton1Click:Connect(function()
  41.     if Side == "Left" then
  42.         Input.Text = Input.Text .. "0"
  43.         Num1 = Num1 .. "0"
  44.         print("C: Added '0' to Left")
  45.     elseif Side == "Right" then
  46.         Input.Text = Input.Text .. "0"
  47.         Num2 = Num2 .. "0"
  48.         print("C: Added '0' to Right")
  49.     end
  50. end)
  51. Btns["1"].MouseButton1Click:Connect(function()
  52.     if Side == "Left" then
  53.         Input.Text = Input.Text .. "1"
  54.         Num1 = Num1 .. "1"
  55.         print("C: Added '1' to Left")
  56.     elseif Side == "Right" then
  57.         Input.Text = Input.Text .. "1"
  58.         Num2 = Num2 .. "1"
  59.         print("C: Added '1' to Right")
  60.     end
  61. end)
  62. Btns["2"].MouseButton1Click:Connect(function()
  63.     if Side == "Left" then
  64.         Input.Text = Input.Text .. "2"
  65.         Num1 = Num1 .. "2"
  66.         print("C: Added '2' to Left")
  67.     elseif Side == "Right" then
  68.         Input.Text = Input.Text .. "2"
  69.         Num2 = Num2 .. "2"
  70.         print("C: Added '2' to Right")
  71.     end
  72. end)
  73. Btns["3"].MouseButton1Click:Connect(function()
  74.     if Side == "Left" then
  75.         Input.Text = Input.Text .. "3"
  76.         Num1 = Num1 .. "3"
  77.         print("C: Added '3' to Left")
  78.     elseif Side == "Right" then
  79.         Input.Text = Input.Text .. "3"
  80.         Num2 = Num2 .. "3"
  81.         print("C: Added '3' to Right")
  82.     end
  83. end)
  84. Btns["4"].MouseButton1Click:Connect(function()
  85.     if Side == "Left" then
  86.         Input.Text = Input.Text .. "4"
  87.         Num1 = Num1 .. "4"
  88.         print("C: Added '4' to Left")
  89.     elseif Side == "Right" then
  90.         Input.Text = Input.Text .. "4"
  91.         Num2 = Num2 .. "4"
  92.         print("C: Added '4' to Right")
  93.     end
  94. end)
  95. Btns["5"].MouseButton1Click:Connect(function()
  96.     if Side == "Left" then
  97.         Input.Text = Input.Text .. "5"
  98.         Num1 = Num1 .. "5"
  99.         print("C: Added '5' to Left")
  100.     elseif Side == "Right" then
  101.         Input.Text = Input.Text .. "5"
  102.         Num2 = Num2 .. "5"
  103.         print("C: Added '5' to Right")
  104.     end
  105. end)
  106. Btns["6"].MouseButton1Click:Connect(function()
  107.     if Side == "Left" then
  108.         Input.Text = Input.Text .. "6"
  109.         Num1 = Num1 .. "6"
  110.         print("C: Added '6' to Left")
  111.     elseif Side == "Right" then
  112.         Input.Text = Input.Text .. "6"
  113.         Num2 = Num2 .. "6"
  114.         print("C: Added '6' to Right")
  115.     end
  116. end)
  117. Btns["7"].MouseButton1Click:Connect(function()
  118.     if Side == "Left" then
  119.         Input.Text = Input.Text .. "7"
  120.         Num1 = Num1 .. "7"
  121.         print("C: Added '7' to Left")
  122.     elseif Side == "Right" then
  123.         Input.Text = Input.Text .. "7"
  124.         Num2 = Num2 .. "7"
  125.         print("C: Added '7' to Right")
  126.     end
  127. end)
  128. Btns["8"].MouseButton1Click:Connect(function()
  129.     if Side == "Left" then
  130.         Input.Text = Input.Text .. "8"
  131.         Num1 = Num1 .. "8"
  132.         print("C: Added '8' to Left")
  133.     elseif Side == "Right" then
  134.         Input.Text = Input.Text .. "8"
  135.         Num2 = Num2 .. "8"
  136.         print("C: Added '8' to Right")
  137.     end
  138. end)
  139. Btns["9"].MouseButton1Click:Connect(function()
  140.     if Side == "Left" then
  141.         Input.Text = Input.Text .. "9"
  142.         Num1 = Num1 .. "9"
  143.         print("C: Added '9' to Left")
  144.     elseif Side == "Right" then
  145.         Input.Text = Input.Text .. "9"
  146.         Num2 = Num2 .. "9"
  147.         print("C: Added '9' to Right")
  148.     end
  149. end)
  150. Btns["*"].MouseButton1Click:Connect(function()
  151.     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
  152.         Input.Text = Input.Text .. "*"
  153.         Operator = "*"
  154.         Side = "Right"
  155.         print("C: Added '*' to Operator")
  156.     else
  157.         print("C: You can't do that!")
  158.     end
  159. end)
  160. Btns["+"].MouseButton1Click:Connect(function()
  161.     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
  162.         Input.Text = Input.Text .. "+"
  163.         Operator = "+"
  164.         Side = "Right"
  165.         print("C: Added '+' to Operator")
  166.     else
  167.         print("C: You can't do that!")
  168.     end
  169. end)
  170. Btns["-"].MouseButton1Click:Connect(function()
  171.     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
  172.         Input.Text = Input.Text .. "-"
  173.         Operator = "-"
  174.         Side = "Right"
  175.         print("C: Added '-' to Operator")
  176.     else
  177.         print("C: You can't do that!")
  178.     end
  179. end)
  180. Btns["/"].MouseButton1Click:Connect(function()
  181.     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
  182.         Input.Text = Input.Text .. "/"
  183.         Operator = "/"
  184.         Side = "Right"
  185.         print("C: Added '/' to Operator")
  186.     else
  187.         print("C: You can't do that!")
  188.     end
  189. end)
  190. Btns["="].MouseButton1Click:Connect(function()
  191.     if string.find(Input.Text, "*") or string.find(Input.Text, "+") or string.find(Input.Text, "-") or string.find(Input.Text, "/") then
  192.         if Operator == "*" then
  193.             Output.Text = tostring(tonumber(Num1)*tonumber(Num2))
  194.         elseif Operator == "+" then
  195.             Output.Text = tostring(tonumber(Num1)+tonumber(Num2))
  196.         elseif Operator == "-" then
  197.             Output.Text = tostring(tonumber(Num1)-tonumber(Num2))
  198.         elseif Operator == "/" then
  199.             Output.Text = tostring(tonumber(Num1)/tonumber(Num2))
  200.         end
  201.         Side = "Left"
  202.         Operator = ""
  203.         Num1 = ""
  204.         Num2 = ""
  205.         print("C: Calculated")
  206.     else
  207.         print("C: You can't do that!")
  208.     end
  209. end)
Add Comment
Please, Sign In to add comment