Skip to content
The Chat Archive
o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)

hi code me a roblox code for shift to run and add some fov effects pls

  1. User

    hi code me a roblox code for shift to run and add some fov effects pls

  2. Assistant

    Hello!

    Here's a Roblox script that enables a "Shift to Run" feature with smooth Field of View (FOV) effects. When the player holds down the Shift key, their character will sprint, and the camera's FOV will increase to give a sense of speed.

    Instructions:

    • Place this script as a LocalScript inside the StarterPlayerScripts folder.

    -- LocalScript inside StarterPlayerScripts
    
    -- Services
    local UserInputService = game:GetService("UserInputService")
    local RunService = game:GetService("RunService")
    local Players = game:GetService("Players")
    
    -- Player and Character
    local player = Players.LocalPlayer
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")
    
    -- Camera
    local camera = workspace.CurrentCamera
    
    -- Walk and Run Speeds
    local walkSpeed = 16       -- Normal walking speed
    local runSpeed = 32        -- Running speed when Shift is held
    
    -- FOV Settings
    local normalFOV = 70       -- Default camera FOV
    local runFOV = 80          -- Camera FOV when running
    local fovSpeed = 2         -- How quickly the FOV changes
    
    -- Running State
    local isRunning = false
    
    -- Update Character and Humanoid References if the Character is Replaced
    player.CharacterAdded:Connect(function(newCharacter)
        character = newCharacter
        humanoid = character:WaitForChild("Humanoid")
    end)
    
    -- Function to Start Running
    local function startRunning()
        isRunning = true
        humanoid.WalkSpeed = runSpeed
    end
    
    -- Function to Stop Running
    local function stopRunning()
        isRunning = false
        humanoid.WalkSpeed = walkSpeed
    end
    
    -- Detect Input Began
    UserInputService.InputBegan:Connect(function(input, gameProcessed)
        if input.KeyCode == Enum.KeyCode.LeftShift then
            startRunning()
        end
    end)
    
    -- Detect Input Ended
    UserInputService.InputEnded:Connect(function(input, gameProcessed)
        if input.KeyCode == Enum.KeyCode.LeftShift then
            stopRunning()
        end
    end)
    
    -- Update FOV Every Frame for Smooth Transition
    RunService.RenderStepped:Connect(function()
        if isRunning then
            -- Increase FOV smoothly up to runFOV
            if camera.FieldOfView < runFOV then
                camera.FieldOfView = math.min(camera.FieldOfView + fovSpeed, runFOV)
            end
        else
            -- Decrease FOV smoothly down to normalFOV
            if camera.FieldOfView > normalFOV then
                camera.FieldOfView = math.max(camera.FieldOfView - fovSpeed, normalFOV)
            end
        end
    end)
    

    Explanation:

    • Services Obtained:

      • UserInputService to detect key presses.
      • RunService to update the camera every frame.
      • Players to get the local player.
    • Variables Set:

      • walkSpeed and runSpeed determine how fast the character moves.
      • normalFOV and runFOV set the camera's field of view for walking and running.
      • fovSpeed controls how quickly the camera FOV changes.
    • Character Updates:

      • Listens for CharacterAdded to ensure the script works if the character respawns.
    • Input Detection:

      • When the Shift key is pressed (InputBegan), startRunning is called.
      • When the Shift key is released (InputEnded), stopRunning is called.
    • Running Functions:

      • startRunning sets isRunning to true and increases the WalkSpeed.
      • stopRunning sets isRunning to false and resets the WalkSpeed.
    • FOV Transition:

      • RenderStepped updates every frame to adjust the camera's FOV smoothly.
      • Uses math.min and math.max to increment or decrement the FOV without overshooting.

    Customization:

    • Adjust Speeds:

      • Change walkSpeed and runSpeed to alter movement speeds.
    • Adjust FOV:

      • Modify normalFOV and runFOV to set the desired field of view.
    • Smoothness:

      • Tweak fovSpeed for smoother or quicker transitions (lower values for smoother changes).

    Placement:

    • Open Roblox Studio.
    • In the Explorer window, find StarterPlayer.
    • Expand StarterPlayer and select StarterPlayerScripts.
    • Right-click StarterPlayerScripts and select Insert Object > LocalScript.
    • Paste the script into the newly created LocalScript.

    Feel free to adjust the variables to suit your game's aesthetic and gameplay feel. This script should provide a solid foundation for a shift-to-run mechanic with dynamic FOV effects.

    Let me know if you need further assistance!