Advertisement
3DCreator

CalculatorScript

Sep 19th, 2021
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. local CalculatorFrame = script.Parent:WaitForChild("CalculatorFrame")
  2. local ButtonsFrame = CalculatorFrame:WaitForChild("ButtonsFrame")
  3. local ListFrame = ButtonsFrame:WaitForChild("ListLayout")
  4. local Input = CalculatorFrame:WaitForChild("Input")
  5. local SymbolData = require(script.SymbolData)
  6. local CalculateModule = require(script.MainModule)
  7. local Number
  8. local Calculation = ""
  9.  
  10. Input.Text = ""
  11.  
  12. function Calculate(String)
  13.     local Ran, Num = pcall( CalculateModule, String )
  14.     if not Ran then Input.Text = "Invalid Calculation" return end
  15.     return Num
  16. end
  17.  
  18. for _, Button in pairs(ListFrame:GetChildren()) do
  19.     if not Button:IsA("TextButton") then continue end
  20.     Button.MouseButton1Click:Connect(function()
  21.         if tonumber(Button.Name) then
  22.             if tonumber(Input.Text) then
  23.                 Input.Text = Input.Text..Button.Name
  24.             else
  25.                 Input.Text = Button.Name
  26.             end
  27.             if Number == nil then
  28.                 Number = tostring(tonumber(Button.Name))
  29.             else
  30.                 Number = Number..tostring(tonumber(Button.Name))
  31.             end
  32.             Calculation = Calculation..tostring(Button.Name)
  33.         elseif Button.Name == "=" then
  34.             Input.Text = Calculate(Calculation)
  35.         elseif Button.Name == "C"then
  36.             Calculation = ""
  37.             Number = nil
  38.             Input.Text = ""
  39.         else
  40.             if Number == nil then return end
  41.             local CodeSymbol = SymbolData[Button.Name]
  42.             if Calculation == nil then
  43.                 return
  44.             else
  45.                 Calculation = Calculation..CodeSymbol
  46.             end
  47.             Number = nil
  48.             Input.Text = Button.Name
  49.         end
  50.     end)
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement