local function Running(speed) if speed > 0.1 and CanSlide then if…
- 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.
- 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 endExplanation:
-
Simplified the Running Condition:
- Combined the running condition (
speed > 0.1 and CanSlide and not Character.Values.Locked.Value) into a single variableisRunning.
- Combined the running condition (
-
Tween Camera:
- If running (
isRunningis true), adjust theFieldOfView. - If not running and not sprinting (ensuring
sprintingis false andCharacter.Values.Locked.Valueis false), reset theFieldOfView.
- If running (
-
Loop through Legs:
- Iterate over both legs and set the
SprintParticle.Enabledproperty based onisRunning.
- Iterate over both legs and set the
This version removes redundancy and makes the code easier to read and maintain.
-
- User
there is still two fors.
- 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 endExplanation:
-
Camera FieldOfView Adjustment:
- Check if
Character.Values.Locked.Valueis false. - Adjust the
FieldOfViewbased on whether the character is running or not.
- Check if
-
Single Loop for Legs:
- Iterate over both
Left LegandRight Leg. - Set the
SprintParticle.Enabledproperty directly based on theisRunningvariable.
- Iterate over both
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.
-