Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- FEATURES:
- - Double-tap space to toggle flight
- - WASD to move, Space to go up, Shift to go down
- - Q/E to adjust speed
- - Visual effects and trail
- ]]--
- -- Services
- local UserInputService = game:GetService("UserInputService")
- local RunService = game:GetService("RunService")
- local Players = game:GetService("Players")
- local TweenService = game:GetService("TweenService")
- -- Variables
- local Player = Players.LocalPlayer
- local Character = Player.Character or Player.CharacterAdded:Wait()
- local Camera = workspace.CurrentCamera
- local flying = false
- local flySpeed = 50
- local minSpeed = 10
- local maxSpeed = 150
- local lastSpace = 0
- local doubleTapTime = 0.3
- local bodyVelocity
- local bodyGyro
- local flightGui
- local controlsGui
- local trailEffect
- local particleEffect
- local particleAttachment
- -- Create permanent controls UI
- local function createControlsUI()
- if controlsGui then controlsGui:Destroy() end
- controlsGui = Instance.new("ScreenGui")
- controlsGui.Name = "FlightControlsGui"
- controlsGui.ResetOnSpawn = false
- controlsGui.Parent = Player.PlayerGui
- local frame = Instance.new("Frame")
- frame.Size = UDim2.new(0, 230, 0, 120)
- frame.Position = UDim2.new(0, 20, 1, -140)
- frame.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
- frame.BackgroundTransparency = 0.6
- frame.BorderSizePixel = 0
- frame.Parent = controlsGui
- local corner = Instance.new("UICorner")
- corner.CornerRadius = UDim.new(0, 10)
- corner.Parent = frame
- local title = Instance.new("TextLabel")
- title.Size = UDim2.new(1, 0, 0, 30)
- title.Position = UDim2.new(0, 0, 0, 5)
- title.BackgroundTransparency = 1
- title.TextColor3 = Color3.fromRGB(255, 255, 255)
- title.Text = "Flight Controls"
- title.Font = Enum.Font.GothamBold
- title.TextSize = 16
- title.Parent = frame
- local controls = {
- "Double-Space: Toggle Flight",
- "WASD: Move Direction",
- "Space: Fly Up",
- "Shift: Fly Down",
- "Q/E: Adjust Speed"
- }
- for i, control in ipairs(controls) do
- local label = Instance.new("TextLabel")
- label.Size = UDim2.new(1, -20, 0, 16)
- label.Position = UDim2.new(0, 10, 0, 30 + (i * 17))
- label.BackgroundTransparency = 1
- label.TextColor3 = Color3.fromRGB(200, 200, 200)
- label.Text = control
- label.TextXAlignment = Enum.TextXAlignment.Left
- label.Font = Enum.Font.Gotham
- label.TextSize = 14
- label.Parent = frame
- end
- -- Animate in
- frame.Position = UDim2.new(-0.3, 0, 1, -140)
- local tween = TweenService:Create(
- frame,
- TweenInfo.new(0.8, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
- {Position = UDim2.new(0, 20, 1, -140)}
- )
- tween:Play()
- end
- -- Create the flight UI
- local function createFlightUI()
- -- Remove any existing UI
- if flightGui then flightGui:Destroy() end
- -- Create new UI
- flightGui = Instance.new("ScreenGui")
- flightGui.Name = "FlightGui"
- flightGui.ResetOnSpawn = false
- flightGui.Parent = Player.PlayerGui
- local frame = Instance.new("Frame")
- frame.Size = UDim2.new(0, 200, 0, 80)
- frame.Position = UDim2.new(1, -220, 0, 20)
- frame.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
- frame.BackgroundTransparency = 0.5
- frame.BorderSizePixel = 0
- frame.Parent = flightGui
- local corner = Instance.new("UICorner")
- corner.CornerRadius = UDim.new(0, 10)
- corner.Parent = frame
- local statusLabel = Instance.new("TextLabel")
- statusLabel.Name = "StatusLabel"
- statusLabel.Size = UDim2.new(1, 0, 0, 30)
- statusLabel.Position = UDim2.new(0, 0, 0, 10)
- statusLabel.BackgroundTransparency = 1
- statusLabel.TextColor3 = Color3.fromRGB(0, 255, 128)
- statusLabel.Text = "FLIGHT: ACTIVE"
- statusLabel.Font = Enum.Font.GothamBold
- statusLabel.TextSize = 18
- statusLabel.Parent = frame
- local speedLabel = Instance.new("TextLabel")
- speedLabel.Name = "SpeedLabel"
- speedLabel.Size = UDim2.new(1, 0, 0, 20)
- speedLabel.Position = UDim2.new(0, 0, 0, 40)
- speedLabel.BackgroundTransparency = 1
- speedLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
- speedLabel.Text = "Speed: " .. flySpeed
- speedLabel.Font = Enum.Font.Gotham
- speedLabel.TextSize = 14
- speedLabel.Parent = frame
- -- Animate UI in
- frame.Position = UDim2.new(1, 50, 0, 20)
- local tween = TweenService:Create(
- frame,
- TweenInfo.new(0.5, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
- {Position = UDim2.new(1, -220, 0, 20)}
- )
- tween:Play()
- -- Return the speed label for updates
- return speedLabel
- end
- -- Update the speed display
- local function updateSpeedDisplay()
- if flightGui and flightGui.Parent then
- local speedLabel = flightGui:FindFirstChild("Frame"):FindFirstChild("SpeedLabel")
- if speedLabel then
- speedLabel.Text = "Speed: " .. flySpeed
- end
- end
- end
- -- Create visual effects
- local function createVisualEffects()
- local rootPart = Character:FindFirstChild("HumanoidRootPart")
- if not rootPart then return end
- -- Create trail effect
- trailEffect = Instance.new("Trail")
- trailEffect.Attachment0 = Instance.new("Attachment", rootPart)
- trailEffect.Attachment0.Position = Vector3.new(0, -1, 0)
- trailEffect.Attachment1 = Instance.new("Attachment", rootPart)
- trailEffect.Attachment1.Position = Vector3.new(0, 1, 0)
- trailEffect.Color = ColorSequence.new({
- ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 255, 255)),
- ColorSequenceKeypoint.new(0.5, Color3.fromRGB(100, 200, 255)),
- ColorSequenceKeypoint.new(1, Color3.fromRGB(0, 100, 255))
- })
- trailEffect.Transparency = NumberSequence.new({
- NumberSequenceKeypoint.new(0, 0.8),
- NumberSequenceKeypoint.new(0.5, 0.5),
- NumberSequenceKeypoint.new(1, 0.8)
- })
- trailEffect.Lifetime = 0.5
- trailEffect.WidthScale = NumberSequence.new({
- NumberSequenceKeypoint.new(0, 0.5),
- NumberSequenceKeypoint.new(0.5, 1),
- NumberSequenceKeypoint.new(1, 0.5)
- })
- trailEffect.Parent = rootPart
- -- Create particle effect
- particleAttachment = Instance.new("Attachment")
- particleAttachment.Position = Vector3.new(0, 0, 0)
- particleAttachment.Parent = rootPart
- particleEffect = Instance.new("ParticleEmitter")
- particleEffect.Color = ColorSequence.new({
- ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 255, 255)),
- ColorSequenceKeypoint.new(1, Color3.fromRGB(100, 200, 255))
- })
- particleEffect.Transparency = NumberSequence.new({
- NumberSequenceKeypoint.new(0, 0.8),
- NumberSequenceKeypoint.new(0.5, 0.5),
- NumberSequenceKeypoint.new(1, 1)
- })
- particleEffect.LightEmission = 0.5
- particleEffect.LightInfluence = 0
- particleEffect.Size = NumberSequence.new({
- NumberSequenceKeypoint.new(0, 0.2),
- NumberSequenceKeypoint.new(1, 0)
- })
- particleEffect.Texture = "rbxassetid://241267873" -- Sparkle particle
- particleEffect.Acceleration = Vector3.new(0, 0, 0)
- particleEffect.Lifetime = NumberRange.new(0.5, 1)
- particleEffect.Rate = 50
- particleEffect.Speed = NumberRange.new(3, 5)
- particleEffect.SpreadAngle = Vector2.new(180, 180)
- particleEffect.Parent = particleAttachment
- end
- -- Remove visual effects
- local function removeVisualEffects()
- if trailEffect then
- if trailEffect.Attachment0 then trailEffect.Attachment0:Destroy() end
- if trailEffect.Attachment1 then trailEffect.Attachment1:Destroy() end
- trailEffect:Destroy()
- trailEffect = nil
- end
- if particleEffect then
- particleEffect:Destroy()
- particleEffect = nil
- end
- if particleAttachment then
- particleAttachment:Destroy()
- particleAttachment = nil
- end
- end
- -- Start flying
- local function startFlying()
- if flying then return end
- -- Wait for character and humanoid
- if not Character or not Character.Parent then
- Character = Player.Character
- if not Character then return end
- end
- local humanoid = Character:FindFirstChildOfClass("Humanoid")
- local rootPart = Character:FindFirstChild("HumanoidRootPart")
- if not humanoid or not rootPart then return end
- -- Set flying flag
- flying = true
- -- Remove existing flight physics objects
- if bodyVelocity then bodyVelocity:Destroy() end
- if bodyGyro then bodyGyro:Destroy() end
- -- Create new flight physics objects
- bodyVelocity = Instance.new("BodyVelocity")
- bodyVelocity.Name = "FlightVelocity"
- bodyVelocity.MaxForce = Vector3.new(10000, 10000, 10000)
- bodyVelocity.Velocity = Vector3.new(0, 0, 0)
- bodyVelocity.Parent = rootPart
- bodyGyro = Instance.new("BodyGyro")
- bodyGyro.Name = "FlightGyro"
- bodyGyro.MaxTorque = Vector3.new(10000, 10000, 10000)
- bodyGyro.P = 1000
- bodyGyro.D = 100
- bodyGyro.CFrame = rootPart.CFrame
- bodyGyro.Parent = rootPart
- -- Create the flight UI
- local speedLabel = createFlightUI()
- -- Create visual effects
- createVisualEffects()
- -- Flight loop
- local connection = RunService.RenderStepped:Connect(function()
- if not flying or not Character.Parent then
- connection:Disconnect()
- return
- end
- -- Control the character's state to prevent falling animation
- if humanoid and humanoid.Parent then
- humanoid:ChangeState(Enum.HumanoidStateType.Physics)
- end
- -- Calculate flight direction
- local moveDirection = Vector3.new(0, 0, 0)
- -- Forward/Backward
- if UserInputService:IsKeyDown(Enum.KeyCode.W) then
- moveDirection = moveDirection + Camera.CFrame.LookVector
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.S) then
- moveDirection = moveDirection - Camera.CFrame.LookVector
- end
- -- Left/Right
- if UserInputService:IsKeyDown(Enum.KeyCode.A) then
- moveDirection = moveDirection - Camera.CFrame.RightVector
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.D) then
- moveDirection = moveDirection + Camera.CFrame.RightVector
- end
- -- Up/Down
- if UserInputService:IsKeyDown(Enum.KeyCode.Space) then
- moveDirection = moveDirection + Vector3.new(0, 1, 0)
- end
- if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) then
- moveDirection = moveDirection - Vector3.new(0, 1, 0)
- end
- -- Apply velocity
- if moveDirection.Magnitude > 0 then
- bodyVelocity.Velocity = moveDirection.Unit * flySpeed
- else
- bodyVelocity.Velocity = Vector3.new(0, 0, 0)
- end
- -- Update bodyGyro to face camera direction
- bodyGyro.CFrame = Camera.CFrame
- end)
- -- Clean up when character is removed
- Character.AncestryChanged:Connect(function(_, parent)
- if not parent and flying then
- stopFlying()
- end
- end)
- end
- -- Stop flying and PROPERLY RESTORE WALKING
- local function stopFlying()
- if not flying then return end
- flying = false
- -- Clean up physics objects
- if bodyVelocity then bodyVelocity:Destroy(); bodyVelocity = nil end
- if bodyGyro then bodyGyro:Destroy(); bodyGyro = nil end
- -- Remove visual effects
- removeVisualEffects()
- -- Find the humanoid
- local humanoid = Character and Character:FindFirstChildOfClass("Humanoid")
- if humanoid then
- -- Important: This sequence of state changes fixes the stuck animation
- task.spawn(function()
- -- First reset to GettingUp (transition state)
- humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
- -- Brief delay to let the physics settle
- task.wait(0.1)
- -- Then force to Running state (allows walking again)
- humanoid:ChangeState(Enum.HumanoidStateType.Running)
- -- If still not working, try landing mode
- task.wait(0.1)
- if humanoid:GetState() ~= Enum.HumanoidStateType.Running then
- humanoid:ChangeState(Enum.HumanoidStateType.Landed)
- task.wait(0.1)
- humanoid:ChangeState(Enum.HumanoidStateType.Running)
- end
- end)
- end
- -- Update flight UI without removing it
- if flightGui and flightGui.Parent then
- local frame = flightGui:FindFirstChild("Frame")
- if frame then
- -- Update status label
- local statusLabel = frame:FindFirstChild("StatusLabel")
- if statusLabel then
- statusLabel.Text = "FLIGHT: DISABLED"
- statusLabel.TextColor3 = Color3.fromRGB(255, 100, 100)
- end
- end
- end
- end
- -- Adjust flight speed
- local function adjustSpeed(change)
- flySpeed = math.clamp(flySpeed + change, minSpeed, maxSpeed)
- updateSpeedDisplay()
- end
- -- Handle input for flight control
- UserInputService.InputBegan:Connect(function(input, gameProcessed)
- if gameProcessed then return end
- -- Double space to toggle flight
- if input.KeyCode == Enum.KeyCode.Space then
- local now = tick()
- if now - lastSpace < doubleTapTime then
- if flying then
- stopFlying()
- else
- startFlying()
- end
- end
- lastSpace = now
- end
- -- Speed control when flying
- if flying then
- if input.KeyCode == Enum.KeyCode.Q then
- adjustSpeed(-10)
- elseif input.KeyCode == Enum.KeyCode.E then
- adjustSpeed(10)
- end
- end
- end)
- -- Show notification on script load
- local function showNotification()
- local notification = Instance.new("ScreenGui")
- notification.Name = "FlightNotification"
- notification.Parent = Player.PlayerGui
- local frame = Instance.new("Frame")
- frame.Size = UDim2.new(0, 300, 0, 100)
- frame.Position = UDim2.new(0.5, -150, 0, -100)
- frame.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
- frame.BackgroundTransparency = 0.5
- frame.BorderSizePixel = 0
- frame.Parent = notification
- local corner = Instance.new("UICorner")
- corner.CornerRadius = UDim.new(0, 10)
- corner.Parent = frame
- local title = Instance.new("TextLabel")
- title.Size = UDim2.new(1, 0, 0, 30)
- title.Position = UDim2.new(0, 0, 0, 10)
- title.BackgroundTransparency = 1
- title.TextColor3 = Color3.fromRGB(255, 255, 255)
- title.Font = Enum.Font.GothamBold
- title.TextSize = 18
- title.Text = "Flight Mode Available"
- title.Parent = frame
- local desc = Instance.new("TextLabel")
- desc.Size = UDim2.new(1, 0, 0, 60)
- desc.Position = UDim2.new(0, 0, 0, 40)
- desc.BackgroundTransparency = 1
- desc.TextColor3 = Color3.fromRGB(200, 200, 200)
- desc.Font = Enum.Font.Gotham
- desc.TextSize = 14
- desc.Text = "Double-tap SPACE to toggle flight\nControls are shown in the bottom-left corner"
- desc.Parent = frame
- -- Animate in
- local tweenIn = TweenService:Create(
- frame,
- TweenInfo.new(0.8, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
- {Position = UDim2.new(0.5, -150, 0, 20)}
- )
- tweenIn:Play()
- -- Animate out after delay
- task.delay(5, function()
- local tweenOut = TweenService:Create(
- frame,
- TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In),
- {Position = UDim2.new(0.5, -150, 0, -100)}
- )
- tweenOut:Play()
- task.delay(0.5, function()
- notification:Destroy()
- end)
- end)
- end
- -- Handle character respawning
- Player.CharacterAdded:Connect(function(newCharacter)
- Character = newCharacter
- if flying then
- stopFlying()
- task.wait(1)
- startFlying()
- end
- end)
- -- Show initial notification
- showNotification()
- -- Create permanent controls display
- createControlsUI()
- print("Complete Flight Script loaded! Double-tap SPACE to toggle flight mode.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement