Hello! I'd be happy to assist you in creating a movable and minimizable GUI in Roblox Studio using Lua. This GUI will include a "Fly" button that enables your character to fly, and a slider to adjust the flight speed from 0 to 10. We'll also make sure it looks modern and visually appealing.
Table of Contents:
- Creating the GUI
- Making the GUI Movable
- Adding Minimize/Maximize Functionality
- Toggle GUI with Right Shift Key
- Implementing the Fly Feature
- Adding a Flight Speed Slider
- Styling the GUI
- Final Notes
1. Creating the GUI
Steps:
-
Insert a ScreenGui:
- In the Explorer window, under
StarterGui, add a new ScreenGui.
- Name it
FlyGui.
-
Add a Main Frame:
- Inside
FlyGui, add a Frame.
- Name it
MainFrame.
- Set its properties:
Size: UDim2.new(0, 300, 0, 200)
Position: UDim2.new(0.5, -150, 0.5, -100) (Centers the frame)
- Adjust
BackgroundColor3, BorderSizePixel, etc., to your preference.
2. Making the GUI Movable
To make the MainFrame draggable:
-
Add a LocalScript to MainFrame:
-
Insert the Following Code:
local UserInputService = game:GetService("UserInputService")
local gui = script.Parent
local dragging, dragInput, dragStart, startPos
local function update(input)
local delta = input.Position - dragStart
gui.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X,
startPos.Y.Scale, startPos.Y.Offset + delta.Y)
end
gui.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = true
dragStart = input.Position
startPos = gui.Position
input.Changed:Connect(function()
if input.UserInputState == Enum.UserInputState.End then
dragging = false
end
end)
end
end)
gui.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
dragInput = input
end
end)
UserInputService.InputChanged:Connect(function(input)
if input == dragInput and dragging then
update(input)
end
end)
3. Adding Minimize/Maximize Functionality
-
Add a Minimize Button:
- Inside
MainFrame, add a TextButton.
- Name it
MinimizeButton.
- Set its properties:
Text: -
Size: UDim2.new(0, 30, 0, 30)
Position: UDim2.new(1, -35, 0, 5)
-
Add a LocalScript to MinimizeButton:
local frame = script.Parent.Parent
local minimized = false
script.Parent.MouseButton1Click:Connect(function()
minimized = not minimized
for _, child in pairs(frame:GetChildren()) do
if child.Name ~= "MinimizeButton" and child.ClassName ~= "UIStroke" and child.ClassName ~= "UICorner" then
child.Visible = not minimized
end
end
if minimized then
script.Parent.Text = "+"
else
script.Parent.Text = "-"
end
end)
4. Toggle GUI with Right Shift Key
-
Add a LocalScript to FlyGui (the ScreenGui):
local UserInputService = game:GetService("UserInputService")
local gui = script.Parent
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.RightShift and not gameProcessedEvent then
gui.Enabled = not gui.Enabled
end
end)
5. Implementing the Fly Feature
-
Add a Fly Button:
- Inside
MainFrame, add a TextButton.
- Name it
FlyButton.
- Set its properties:
Text: Fly
Size: UDim2.new(0, 100, 0, 50)
Position: UDim2.new(0.5, -50, 0, 50)
-
Add a LocalScript to FlyButton:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local flying = false
-- Create a FlightSpeed NumberValue
local flightSpeedValue = Instance.new("NumberValue")
flightSpeedValue.Name = "FlightSpeed"
flightSpeedValue.Value = 50 -- Default speed
flightSpeedValue.Parent = player
-- Function to start/stop flying
local function toggleFly()
flying = not flying
if flying then
script.Parent.Text = "Stop Flying"
local bodyGyro = Instance.new("BodyGyro", humanoidRootPart)
local bodyVelocity = Instance.new("BodyVelocity", humanoidRootPart)
bodyGyro.P = 9e4
bodyGyro.maxTorque = Vector3.new(9e9, 9e9, 9e9)
bodyGyro.cframe = humanoidRootPart.CFrame
bodyVelocity.velocity = Vector3.new(0, 0, 0)
bodyVelocity.maxForce = Vector3.new(9e9, 9e9, 9e9)
local runService = game:GetService("RunService")
local flyConnection
flyConnection = runService.Stepped:Connect(function()
local controlDir = Vector3.new()
local userInput = game:GetService("UserInputService")
if userInput:IsKeyDown(Enum.KeyCode.W) then
controlDir = controlDir + workspace.CurrentCamera.CFrame.LookVector
end
if userInput:IsKeyDown(Enum.KeyCode.S) then
controlDir = controlDir - workspace.CurrentCamera.CFrame.LookVector
end
if userInput:IsKeyDown(Enum.KeyCode.A) then
controlDir = controlDir - workspace.CurrentCamera.CFrame.RightVector
end
if userInput:IsKeyDown(Enum.KeyCode.D) then
controlDir = controlDir + workspace.CurrentCamera.CFrame.RightVector
end
if userInput:IsKeyDown(Enum.KeyCode.Space) then
controlDir = controlDir + Vector3.new(0, 1, 0)
end
if userInput:IsKeyDown(Enum.KeyCode.LeftShift) then
controlDir = controlDir - Vector3.new(0, 1, 0)
end
controlDir = controlDir.Unit
if controlDir.Magnitude > 0 then
bodyVelocity.velocity = controlDir * flightSpeedValue.Value
else
bodyVelocity.velocity = Vector3.new(0, 0, 0)
end
end)
-- Listen for when the player stops flying
local flyEndConnection
flyEndConnection = script.Parent.MouseButton1Click:Connect(function()
flying = false
flyConnection:Disconnect()
flyEndConnection:Disconnect()
bodyGyro:Destroy()
bodyVelocity:Destroy()
script.Parent.Text = "Fly"
end)
end
end
script.Parent.MouseButton1Click:Connect(toggleFly)
6. Adding a Flight Speed Slider
Since Roblox doesn't have a built-in slider, we'll create one using GUI elements.
-
Create the Slider Bar:
- Inside
MainFrame, add a Frame.
- Name it
SliderFrame.
- Set its properties:
Size: UDim2.new(0, 200, 0, 20)
Position: UDim2.new(0.5, -100, 0, 120)
- Adjust background color as desired.
-
Add the Slider Button:
- Inside
SliderFrame, add an ImageButton.
- Name it
SliderButton.
- Set its properties:
Size: UDim2.new(0, 20, 1, 0)
Position: UDim2.new(0, 0, 0, 0)
BackgroundColor3: Choose a distinct color.
- Adjust other properties as desired.
-
Add a Speed Display Label:
- Inside
MainFrame, add a TextLabel.
- Name it
SpeedLabel.
- Set its properties:
Text: Speed: 5.0
Size: UDim2.new(0, 100, 0, 30)
Position: UDim2.new(0.5, -50, 0, 150)
- Adjust font, color, etc.
-
Add a LocalScript to SliderButton:
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local dragging = false
local sliderButton = script.Parent
local sliderFrame = sliderButton.Parent
local speedLabel = sliderFrame.Parent:FindFirstChild("SpeedLabel")
local flightSpeedValue = player:FindFirstChild("FlightSpeed")
sliderButton.MouseButton1Down:Connect(function()
dragging = true
end)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
dragging = false
end
end)
UserInputService.InputChanged:Connect(function(input)
if dragging and input.UserInputType == Enum.UserInputType.MouseMovement then
local sliderStart = sliderFrame.AbsolutePosition.X
local sliderEnd = sliderStart + sliderFrame.AbsoluteSize.X
local mousePos = UserInputService:GetMouseLocation().X
local newX = math.clamp(mousePos, sliderStart, sliderEnd)
local percent = (newX - sliderStart) / sliderFrame.AbsoluteSize.X
sliderButton.Position = UDim2.new(percent, -sliderButton.AbsoluteSize.X / 2, 0, 0)
local minSpeed = 0
local maxSpeed = 10
local currentSpeed = minSpeed + (maxSpeed - minSpeed) * percent
flightSpeedValue.Value = currentSpeed * 10 -- Multiply as needed for sensitivity
speedLabel.Text = string.format("Speed: %.1f", currentSpeed)
end
end)
7. Styling the GUI
To make the GUI modern and visually appealing:
-
Add UICorner to GUI Elements:
- For
MainFrame, FlyButton, MinimizeButton, SliderFrame, and SliderButton, add a UICorner instance.
- Set
CornerRadius to UDim.new(0, 8) for smooth corners.
-
Add UIGradient (Optional):
- For background elements, add a
UIGradient to create a gradient effect.
- Configure the
Color property with your desired gradient colors.
-
Adjust Colors and Fonts:
- Set
BackgroundColor3, TextColor3, and fonts to modern styles.
- Consider using a sans-serif font like
SourceSans for a clean look.
-
Add UIStroke (Optional):
- Add
UIStroke instances to elements for outlines.
- Set
Color and Thickness as desired.
Example Style Adjustments:
-- Setting background color
MainFrame.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
FlyButton.BackgroundColor3 = Color3.fromRGB(50, 150, 250)
MinimizeButton.BackgroundColor3 = Color3.fromRGB(100, 100, 100)
SliderFrame.BackgroundColor3 = Color3.fromRGB(80, 80, 80)
SliderButton.BackgroundColor3 = Color3.fromRGB(200, 200, 200)
SpeedLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
8. Final Notes
- Testing: Make sure to test each feature individually to ensure they work correctly.
- Organize Scripts: Keep your scripts well-organized and commented for easier maintenance.
- User Experience: Ensure that the GUI elements are responsive and user-friendly.
- Optimization: Avoid unnecessary computations within loops to optimize performance.
- Additional Features: You can expand on this by adding animations, sounds, or other interactive elements.
I hope this helps you create the GUI for your Roblox game! If you have any questions or need further assistance, feel free to ask. Good luck with your game development!