Skip to content
The Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

local function Running(speed) if speed > 0.1 and CanSlide then if…

  1. User

    local function Running(speed) if speed > 0.1 and CanSlide then if Character.Values.Locked.Value == false then TweenService:Create(Camera, TweenInfo.new(0.7, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = CameraSettings.FOV.Value - 10}):Play() end for _, leg in {"Left Leg", "Right Leg"} do local legObj = Character[leg] if legObj then local SprintParticle = legObj:FindFirstChild("Sprint") if SprintParticle then SprintParticle.Enabled = true end end end else if Character.Values.Locked.Value == false then if not sprinting then TweenService:Create(Camera, TweenInfo.new(0.7, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = CameraSettings.FOV.Value}):Play() end end for _, leg in {"Left Leg", "Right Leg"} do local legObj = Character[leg] if legObj then local SprintParticle = legObj:FindFirstChild("Sprint") if SprintParticle then SprintParticle.Enabled = false end end end end end

    there is no point foran ifelse statement if in both cases they do kinda the most same thing. it's redundant.

  2. Assistant

    I understand your concern regarding the redundancy in the if-else statement. You can simplify the code by merging the common parts and breaking down the different parts more efficiently. Here's a refactored version of the function:

    local function Running(speed)
    	local isRunning = speed > 0.1 and CanSlide and not Character.Values.Locked.Value
    	
    	if isRunning then
    		TweenService:Create(Camera, TweenInfo.new(0.7, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = CameraSettings.FOV.Value - 10}):Play()
    	elseif not sprinting and not Character.Values.Locked.Value then
    		TweenService:Create(Camera, TweenInfo.new(0.7, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = CameraSettings.FOV.Value}):Play()
    	end
    	
    	for _, leg in {"Left Leg", "Right Leg"} do
    		local legObj = Character[leg]
    		if legObj then
    			local SprintParticle = legObj:FindFirstChild("Sprint")
    			if SprintParticle then
    				SprintParticle.Enabled = isRunning
    			end
    		end
    	end
    end
    

    Explanation:

    1. Simplified the Running Condition:

      • Combined the running condition (speed > 0.1 and CanSlide and not Character.Values.Locked.Value) into a single variable isRunning.
    2. Tween Camera:

      • If running (isRunning is true), adjust the FieldOfView.
      • If not running and not sprinting (ensuring sprinting is false and Character.Values.Locked.Value is false), reset the FieldOfView.
    3. Loop through Legs:

      • Iterate over both legs and set the SprintParticle.Enabled property based on isRunning.

    This version removes redundancy and makes the code easier to read and maintain.

  3. User

    there is still two fors.

  4. Assistant

    To further simplify the code and avoid redundant loops, you can consolidate the logic within a single loop while still checking the conditions effectively. Here's how you can do it:

    local function Running(speed)
    	local isRunning = speed > 0.1 and CanSlide and not Character.Values.Locked.Value
    	
    	-- Handle the Camera FieldOfView adjustment
    	if Character.Values.Locked.Value == false then
    		if isRunning then
    			TweenService:Create(Camera, TweenInfo.new(0.7, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = CameraSettings.FOV.Value - 10}):Play()
    		elseif not sprinting then
    			TweenService:Create(Camera, TweenInfo.new(0.7, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = CameraSettings.FOV.Value}):Play()
    		end
    	end
    	
    	-- Loop through both legs and handle the SprintParticle
    	for _, leg in {"Left Leg", "Right Leg"} do
    		local legObj = Character[leg]
    		if legObj then
    			local SprintParticle = legObj:FindFirstChild("Sprint")
    			if SprintParticle then
    				SprintParticle.Enabled = isRunning
    			end
    		end
    	end
    end
    

    Explanation:

    1. Camera FieldOfView Adjustment:

      • Check if Character.Values.Locked.Value is false.
      • Adjust the FieldOfView based on whether the character is running or not.
    2. Single Loop for Legs:

      • Iterate over both Left Leg and Right Leg.
      • Set the SprintParticle.Enabled property directly based on the isRunning variable.

    By combining and simplifying the camera adjustment and leg handling into a single loop and condition checks, the code is now more concise and easier to understand without redundant for-loops.