0% found this document useful (0 votes)
2K views4 pages

Roblox Fly Script with GUI Controls

The document is a Lua script for a flying GUI in a Roblox game, allowing players to toggle flying and adjust speed. It includes user input handling for movement directions and animations based on player actions. The script creates a user interface with buttons to control flying and display the current speed, along with the necessary physics components for flying functionality.

Uploaded by

leoelijahcruz142
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views4 pages

Roblox Fly Script with GUI Controls

The document is a Lua script for a flying GUI in a Roblox game, allowing players to toggle flying and adjust speed. It includes user input handling for movement directions and animations based on player actions. The script creates a user interface with buttons to control flying and display the current speed, along with the necessary physics components for flying functionality.

Uploaded by

leoelijahcruz142
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

local savedSpeed = 50

local function gui()


local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local player = [Link]
local character = [Link] or [Link]:Wait()
local humanoid = character:WaitForChild("Humanoid")
local HRP = character:WaitForChild("HumanoidRootPart")
local Camera = [Link]

local baseSpeed = savedSpeed


local flySpeed = baseSpeed
local flying = false
local forwardHold = 0
local inputFlags = { forward = false, back = false, left = false, right =
false, up = false, down = false }

local bodyVelocity = [Link]("BodyVelocity")


[Link] = [Link](1e5, 1e5, 1e5)

local bodyGyro = [Link]("BodyGyro")


[Link] = [Link](1e5, 1e5, 1e5)

local screenGui = [Link]("ScreenGui")


[Link] = "FlyScreenGui"
[Link] = false
[Link] = player:WaitForChild("PlayerGui")

local toggleButton = [Link]("TextButton")


[Link] = "ToggleFlyButton"
[Link] = "Fly OFF"
[Link] = [Link](0, 100, 0, 50)
[Link] = [Link](1, -220, 0, 10)
toggleButton.BackgroundColor3 = [Link](40, 40, 40)
toggleButton.TextColor3 = [Link](255, 255, 255)
[Link] = [Link]
[Link] = true
[Link] = 0.2
[Link] = screenGui

local speedBox = [Link]("TextBox")


[Link] = "SpeedBox"
[Link] = tostring(baseSpeed)
[Link] = [Link](0, 100, 0, 50)
[Link] = [Link](1, -110, 0, 10)
speedBox.BackgroundColor3 = [Link](40, 40, 40)
speedBox.TextColor3 = [Link](255, 255, 255)
[Link] = [Link]
[Link] = true
[Link] = 0.2
[Link] = screenGui

local function newAnim(id)


local anim = [Link]("Animation")
[Link] = "rbxassetid://" .. id
return anim
end
local animations = {
forward = newAnim(90872539),
up = newAnim(90872539),
right1 = newAnim(136801964),
right2 = newAnim(142495255),
left1 = newAnim(136801964),
left2 = newAnim(142495255),
flyLow1 = newAnim(97169019),
flyLow2 = newAnim(282574440),
flyFast = newAnim(282574440),
back1 = newAnim(136801964),
back2 = newAnim(106772613),
back3 = newAnim(42070810),
back4 = newAnim(214744412),
down = newAnim(233322916),
idle1 = newAnim(97171309)
}

local tracks = {}
for name, anim in pairs(animations) do
tracks[name] = humanoid:LoadAnimation(anim)
end

local function stopAll()


for _, track in pairs(tracks) do
track:Stop()
end
end

local function startFlying()


flying = true
forwardHold = 0
flySpeed = baseSpeed
[Link] = HRP
[Link] = HRP
[Link] = true
end

local function stopFlying()


flying = false
[Link] = nil
[Link] = nil
[Link] = false
stopAll()
end

toggleButton.MouseButton1Click:Connect(function()
if flying then
stopFlying()
[Link] = "Fly OFF"
else
startFlying()
[Link] = "Fly ON"
end
end)

[Link]:Connect(function()
local num = tonumber([Link])
if num and num > 0 then
baseSpeed = num
savedSpeed = num
if flying then flySpeed = baseSpeed end
else
[Link] = tostring(baseSpeed)
end
end)

[Link]:Connect(function(input, gameProcessed)
if gameProcessed then return end
if [Link] == [Link].W then [Link] = true end
if [Link] == [Link].S then [Link] = true end
if [Link] == [Link].A then [Link] = true end
if [Link] == [Link].D then [Link] = true end
if [Link] == [Link].E then [Link] = true end
if [Link] == [Link].Q then [Link] = true end
end)

[Link]:Connect(function(input)
if [Link] == [Link].W then [Link] = false end
if [Link] == [Link].S then [Link] = false end
if [Link] == [Link].A then [Link] = false end
if [Link] == [Link].D then [Link] = false end
if [Link] == [Link].E then [Link] = false end
if [Link] == [Link].Q then [Link] = false end
end)

[Link]:Connect(function(dt)
if not flying then return end

if not [Link] then forwardHold = 0 end

local dir = [Link]


local camCF = [Link]

if [Link] then dir += [Link] end


if [Link] then dir -= [Link] end
if [Link] then dir -= [Link] end
if [Link] then dir += [Link] end
if [Link] then dir += [Link] end
if [Link] then dir -= [Link] end

if [Link] > 0 then dir = [Link] end

[Link] = dir * flySpeed


[Link] = camCF

-- Animation Logic
if [Link] then
if not [Link] then stopAll(); [Link]:Play() end
elseif [Link] then
if not [Link] then stopAll(); [Link]:Play() end
elseif [Link] then
if not [Link] then
stopAll()
tracks.left1:Play(); [Link] = 2.0;
tracks.left1:AdjustSpeed(0)
tracks.left2:Play(); [Link] = 0.5;
tracks.left2:AdjustSpeed(0)
end
elseif [Link] then
if not [Link] then
stopAll()
tracks.right1:Play(); [Link] = 1.1;
tracks.right1:AdjustSpeed(0)
tracks.right2:Play(); [Link] = 0.5;
tracks.right2:AdjustSpeed(0)
end
elseif [Link] then
if not [Link] then
stopAll()
tracks.back1:Play(); [Link] = 5.3;
tracks.back1:AdjustSpeed(0)
tracks.back2:Play(); tracks.back2:AdjustSpeed(0)
tracks.back3:Play(); [Link] = 0.8;
tracks.back3:AdjustSpeed(0)
tracks.back4:Play(); [Link] = 1;
tracks.back4:AdjustSpeed(0)
end
elseif [Link] then
forwardHold += dt
if forwardHold >= 3 then
if not [Link] then
stopAll()
flySpeed = baseSpeed * 1.3
[Link]:Play(); [Link]:AdjustSpeed(0.05)
end
else
if not [Link] then
stopAll()
flySpeed = baseSpeed
tracks.flyLow1:Play()
tracks.flyLow2:Play()
end
end
else
if not [Link] then
stopAll()
tracks.idle1:Play(); tracks.idle1:AdjustSpeed(0)
end
end
end)
end

gui()

Common questions

Powered by AI

The script uses the `UserInputService` to listen for `InputBegan` and `InputEnded` events, detecting when specific keys (W, A, S, D, E, Q) are pressed or released. These keys manipulate the `inputFlags` table, setting respective directional flags (e.g., `inputFlags.forward` for W) to true or false. Based on these flags, the `RenderStepped` function dynamically adjusts the `dir` vector and the player's flying speed, eventually specifying the player's flying direction and animation selection .

The script illustrates several programming paradigms, notably event-driven programming, through its extensive use of listeners and callbacks on user input (`InputBegan`, `InputEnded`). It also demonstrates object-oriented programming by utilizing object instances like `BodyVelocity`, `BodyGyro`, and GUI elements, treating them as discrete objects with properties and methods. Additionally, the script applies the procedural paradigm via a sequence of commands to execute the flight mechanism's logic .

The script dynamically adjusts animation speeds using the `AdjustSpeed()` method for specific tracks. When flying conditions change, such as pressing forward for an extended period, the `flyFast` animation is played, and its speed is adjusted using `tracks.flyFast:AdjustSpeed(0.05)`. This customized control allows the script to synchronize animation pacing with flight speed, ensuring animations accurately reflect the character's motion state .

Improving the script to handle simultaneous input directions could involve refining the logic for vector calculations in the `RenderStepped` function. Currently, it adds or subtracts vectors directly, which might not appropriately prioritize or normalize combined directions. Implementation of additional vector math to ensure smooth transitions or use of interpolated movement strategies could enhance responsiveness. Additionally, improving the `stopAll()` function to efficiently resume relevant animations would help manage simultaneous inputs better .

The GUI in the script provides users with a visual interface to control flight status and adjust the flying speed. It includes a `ScreenGui` that houses a `TextButton` labeled 'Fly OFF/ON' to toggle the flying state and a `TextBox` known as 'SpeedBox' to input and alter the flying speed. The GUI structures facilitate ease of interaction whereby clicking the button starts or stops flying and modifying the text in the `TextBox` changes `baseSpeed`, subsequently altering `flySpeed` if flying is activated .

The script employs `BodyVelocity` to control the player's movement speed and direction in the 3D space by setting its `Velocity` property to the calculated direction vector multiplied by `flySpeed`. `BodyGyro` is utilized to maintain orientation alignment with the camera, as it sets its `CFrame` to match the camera's `CFrame`, allowing the player to face the intended direction while flying. Both components are parented to `HumanoidRootPart` when flying is initiated and are removed upon stopping, simulating realistic flight dynamics .

When flying is toggled off, the script invokes the `stopFlying()` function, which sets the `flying` flag to false, removes `BodyVelocity` and `BodyGyro` instances from `HumanoidRootPart`, and restores `humanoid.PlatformStand` to false. This transition ensures no residual forces affect the player’s character. All currently playing animations are halted through the `stopAll()` function, allowing for a smooth cessation of motion without abrupt changes .

The script monitors if none of the directional input flags are triggered, and if all are false, it defaults to playing the `idle1` animation. This is managed within the `RenderStepped` function by checking the absence of active input flags, at which point `stopAll()` is called, and the `idle1` animation is played, ensuring the player doesn’t appear static or glitchy in idle state, maintaining visual continuity .

The script utilizes a series of input flags to determine which direction the player is intending to move and selects an animation accordingly. The `RenderStepped` function handles this by checking `inputFlags` to see which keys are pressed (e.g., forward, back, left, right, up, down). If a direction is detected such as forward (`inputFlags.forward`), and specific conditions are met (e.g., `forwardHold` duration), it plays the respective animation track (e.g., `flyFast` or `flyLow1`). The function uses `stopAll()` to halt all animations before playing a new one, ensuring only one animation plays at any time, with adjustments in `flySpeed` as needed .

The script checks `gameProcessed` within the `InputBegan` event to determine if the input has already been handled by the game's default systems. This ensures the script doesn’t redundantly react to inputs like those associated with in-game actions (e.g., opening a menu). Omitting this check could lead to inconsistent or unintended behavior where the script triggers actions despite being unnecessary, potentially conflicting with or overriding game controls .

You might also like