Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Create a ScreenGui to hold the calculator GUI
- local gui = Instance.new("ScreenGui")
- gui.Name = "CalculatorGUI"
- gui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
- -- Create a Frame to hold the calculator buttons
- local frame = Instance.new("Frame")
- frame.Size = UDim2.new(0, 200, 0, 250)
- frame.Position = UDim2.new(0.5, -100, 0.5, -125)
- frame.BackgroundTransparency = 0.5
- frame.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
- frame.Parent = gui
- -- Create buttons for numbers and operators
- local buttons = {
- "7", "8", "9", "/",
- "4", "5", "6", "*",
- "1", "2", "3", "-",
- "0", ".", "=", "+"
- }
- -- Function to handle button click events
- local function onButtonClick(value)
- if value == "=" then
- -- Perform calculation
- local expression = gui.InputBox.Text
- local result = loadstring("return " .. expression)()
- gui.InputBox.Text = tostring(result)
- elseif value == "C" then
- -- Clear input
- gui.InputBox.Text = ""
- else
- -- Append value to input
- gui.InputBox.Text = gui.InputBox.Text .. value
- end
- end
- -- Create buttons dynamically
- local buttonSize = UDim2.new(0.25, -5, 0.2, -5)
- local buttonPosition = UDim2.new(0.25, 0, 0.2, 0)
- for i, value in ipairs(buttons) do
- local button = Instance.new("TextButton")
- button.Size = buttonSize
- button.Position = buttonPosition
- button.Text = value
- button.Parent = frame
- button.MouseButton1Click:Connect(function()
- onButtonClick(value)
- end)
- -- Update button position
- buttonPosition = buttonPosition + UDim2.new(0.25, 5, 0, 0)
- if i % 4 == 0 then
- buttonPosition = UDim2.new(0.25, 0, buttonPosition.Y.Scale + 0.2, 0)
- end
- end
- -- Create an InputBox to display and input the calculation
- local inputBox = Instance.new("TextBox")
- inputBox.Size = UDim2.new(0.9, 0, 0.15, 0)
- inputBox.Position = UDim2.new(0.05, 0, 0.05, 0)
- inputBox.Text = ""
- inputBox.FontSize = Enum.FontSize.Size24
- inputBox.TextColor3 = Color3.fromRGB(0, 0, 0)
- inputBox.BackgroundTransparency = 0.5
- inputBox.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
- inputBox.Parent = frame
- gui.InputBox = inputBox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement