Advertisement
TheHatBoys

caculator

Jun 27th, 2023
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | Source Code | 0 0
  1. -- Create a ScreenGui to hold the calculator GUI
  2. local gui = Instance.new("ScreenGui")
  3. gui.Name = "CalculatorGUI"
  4. gui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
  5.  
  6. -- Create a Frame to hold the calculator buttons
  7. local frame = Instance.new("Frame")
  8. frame.Size = UDim2.new(0, 200, 0, 250)
  9. frame.Position = UDim2.new(0.5, -100, 0.5, -125)
  10. frame.BackgroundTransparency = 0.5
  11. frame.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
  12. frame.Parent = gui
  13.  
  14. -- Create buttons for numbers and operators
  15. local buttons = {
  16. "7", "8", "9", "/",
  17. "4", "5", "6", "*",
  18. "1", "2", "3", "-",
  19. "0", ".", "=", "+"
  20. }
  21.  
  22. -- Function to handle button click events
  23. local function onButtonClick(value)
  24. if value == "=" then
  25. -- Perform calculation
  26. local expression = gui.InputBox.Text
  27. local result = loadstring("return " .. expression)()
  28. gui.InputBox.Text = tostring(result)
  29. elseif value == "C" then
  30. -- Clear input
  31. gui.InputBox.Text = ""
  32. else
  33. -- Append value to input
  34. gui.InputBox.Text = gui.InputBox.Text .. value
  35. end
  36. end
  37.  
  38. -- Create buttons dynamically
  39. local buttonSize = UDim2.new(0.25, -5, 0.2, -5)
  40. local buttonPosition = UDim2.new(0.25, 0, 0.2, 0)
  41.  
  42. for i, value in ipairs(buttons) do
  43. local button = Instance.new("TextButton")
  44. button.Size = buttonSize
  45. button.Position = buttonPosition
  46. button.Text = value
  47. button.Parent = frame
  48.  
  49. button.MouseButton1Click:Connect(function()
  50. onButtonClick(value)
  51. end)
  52.  
  53. -- Update button position
  54. buttonPosition = buttonPosition + UDim2.new(0.25, 5, 0, 0)
  55. if i % 4 == 0 then
  56. buttonPosition = UDim2.new(0.25, 0, buttonPosition.Y.Scale + 0.2, 0)
  57. end
  58. end
  59.  
  60. -- Create an InputBox to display and input the calculation
  61. local inputBox = Instance.new("TextBox")
  62. inputBox.Size = UDim2.new(0.9, 0, 0.15, 0)
  63. inputBox.Position = UDim2.new(0.05, 0, 0.05, 0)
  64. inputBox.Text = ""
  65. inputBox.FontSize = Enum.FontSize.Size24
  66. inputBox.TextColor3 = Color3.fromRGB(0, 0, 0)
  67. inputBox.BackgroundTransparency = 0.5
  68. inputBox.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
  69. inputBox.Parent = frame
  70. gui.InputBox = inputBox
  71.  
Tags: scriot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement