Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

give me Lua script fly

  1. User

    give me Lua script fly

  2. Assistant

    Creating a "fly" script in Lua typically refers to a script that allows a player to fly, often used in Roblox games. Here's a simple example of a fly script for Roblox. Note that using such scripts might be against the rules of certain games or platforms, so be sure to use scripts responsibly and ethically.

    -- Simple Fly Script for Roblox
    -- Attach this script to a LocalScript
    
    local UserInputService = game:GetService("UserInputService")
    local RunService = game:GetService("RunService")
    
    local player = game.Players.LocalPlayer
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")
    local rootPart = character:WaitForChild("HumanoidRootPart")
    
    local flying = false
    local speed = 50
    local velocity = Vector3.new(0,0,0)
    
    local flightDirection = Vector3.new(0,0,0)
    
    -- Listen for input to toggle flying
    UserInputService.InputBegan:Connect(function(input, gameProcessed)
        if gameProcessed then return end
        if input.KeyCode == Enum.KeyCode.E then -- Press E to toggle fly
            flying = not flying
            if flying then
                humanoid.PlatformStand = true -- to disable default physics
            else
                humanoid.PlatformStand = false
                rootPart.Velocity = Vector3.new(0,0,0)
            end
        end
    end)
    
    -- Movement keys to control flying direction
    local directions = {
        [Enum.KeyCode.W] = Vector3.new(0,0,-1),
        [Enum.KeyCode.S] = Vector3.new(0,0,1),
        [Enum.KeyCode.A] = Vector3.new(-1,0,0),
        [Enum.KeyCode.D] = Vector3.new(1,0,0),
        [Enum.KeyCode.Space] = Vector3.new(0,1,0),
        [Enum.KeyCode.LeftControl] = Vector3.new(0,-1,0)
    }
    
    local keysPressed = {}
    
    UserInputService.InputBegan:Connect(function(input, gameProcessed)
        if directions[input.KeyCode] then
            keysPressed[input.KeyCode] = true
        end
    end)
    
    UserInputService.InputEnded:Connect(function(input, gameProcessed)
        if directions[input.KeyCode] then
            keysPressed[input.KeyCode] = false
        end
    end)
    
    RunService.RenderStepped:Connect(function()
        if flying then
            flightDirection = Vector3.new(0,0,0)
            for key, vector in pairs(directions) do
                if keysPressed[key] then
                    flightDirection = flightDirection + vector
                end
            end
            if flightDirection.Magnitude > 0 then
                flightDirection = flightDirection.Unit
                -- Move relative to the camera's look direction
                local camera = workspace.CurrentCamera
                local camCFrame = camera.CFrame
                local moveVector = (camCFrame.RightVector * flightDirection.X) + (Vector3.new(0,flightDirection.Y,0)) + (camCFrame.LookVector * flightDirection.Z)
                rootPart.Velocity = moveVector.Unit * speed
            else
                rootPart.Velocity = Vector3.new(0,0,0)
            end
        end
    end)
    

    How to use:

    • Insert a LocalScript into StarterPlayerScripts.
    • Copy-paste this code.
    • Press "E" during play to toggle flying mode.
    • Use WASD to move horizontally, Space to go up, Left Ctrl to go down.

    Let me know if you want a version for another environment!