Optimizing this script

Hey hey devforum, how would I optimize this script and maybe make it more efficient or more easily readable? I especially don’t really like the ‘Dashing’ function.

--||Services||--
local StarterPlayer = game:GetService("StarterPlayer")
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local Players = game:GetService("Players")
local TS = game:GetService("TweenService")

--||Event||--
local DashEvent = script:WaitForChild("Dash")

--||Player||--
local plr = Players.LocalPlayer
local char = plr.Character
local hum = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")
local Mouse = plr:GetMouse()

--||Values||--
local WButton, SButton, AButton, DButton = false, false, false, false 
local DashDebouce = false

--||Animations||--
local Front_Track = hum.Animator:LoadAnimation(RS.Animations.Player.Dash.Front)
local Back_Track = hum.Animator:LoadAnimation(RS.Animations.Player.Dash.Back)
local Left_Track = hum.Animator:LoadAnimation(RS.Animations.Player.Dash.Left)
local Right_Track = hum.Animator:LoadAnimation(RS.Animations.Player.Dash.Right)

--||Settings||--
local Cooldown = 2 --remember we are also adding some time to it
local Distance = 50
local Added_Distance = 50 --make sure thats the same as the normal velocity

local Front_Time, Back_Time, Left_Time, Right_Time = .4, .8, 0.4, 0.4 --the time it takes to dash

------------------------------------------------------------------------------------------------------------------




--//Checking for diffrent values
local function CheckingForValues()
	if hum.Health <= 0 or DashDebouce then return false else return true end
end


--//Creating the dash
local function Dashing()
	if not WButton and not SButton and not AButton and not DButton then return end
	Added_Distance = Distance
	
	local BV = Instance.new("BodyVelocity", hrp)
	BV.Name = "DashPosition"
	BV.MaxForce = Vector3.new(50000, 0, 50000)
	
	DashDebouce = true
	DashEvent:FireServer()
	
	hum.WalkSpeed = 0
	hum.JumpHeight = 0
	
	local connection
	
	--checking which button was held down
	if WButton == true then	
		Added_Distance += 5 --this is just so the dash is not as strong as the side dashes

		Front_Track:Play()
		
		local tweenInfo = TweenInfo.new(Front_Time, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
		local tween = TS:Create(BV, tweenInfo, {Velocity = hrp.CFrame.LookVector * Added_Distance})
		tween:Play()

		connection = RunService.RenderStepped:Connect(function()
			if not BV then
				connection:Disconnect()
			else
				BV.Velocity = hrp.CFrame.LookVector * Added_Distance
			end
		end)

		task.wait(Front_Time)
		BV:Destroy()
		connection:Disconnect()
		
		task.delay(.5,function() --kind of a uhh idk forgot the name but like you can't move for a bit after the front dash
			hum.WalkSpeed = StarterPlayer.CharacterWalkSpeed
			hum.JumpHeight = StarterPlayer.CharacterJumpHeight
		end)	
		
	elseif SButton == true then	
		Added_Distance -= 10 --this is just so the dash is not as strong as the side dashes

		Back_Track:Play()
		
		local tweenInfo = TweenInfo.new(Back_Time, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
		local tween = TS:Create(BV, tweenInfo, {Velocity = hrp.CFrame.LookVector * -Added_Distance})
		tween:Play()

		connection = RunService.RenderStepped:Connect(function()
			if not BV then
				connection:Disconnect()
			else
				BV.Velocity = hrp.CFrame.LookVector * -Added_Distance
			end
		end)

		task.wait(Back_Time)
		BV:Destroy()
		connection:Disconnect()
		
		hum.WalkSpeed = StarterPlayer.CharacterWalkSpeed
		hum.JumpHeight = StarterPlayer.CharacterJumpHeight

	elseif AButton == true then
		Left_Track:Play()	

		local tweenInfo = TweenInfo.new(Left_Time, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
		local tween = TS:Create(BV, tweenInfo, {Velocity = hrp.CFrame.RightVector * -Added_Distance})
		tween:Play()

		connection = RunService.RenderStepped:Connect(function()
			if not BV then
				connection:Disconnect()
			else
				BV.Velocity = hrp.CFrame.RightVector * -Added_Distance
			end
		end)

		task.wait(Left_Time)
		BV:Destroy()
		connection:Disconnect()
		
		hum.WalkSpeed = StarterPlayer.CharacterWalkSpeed
		hum.JumpHeight = StarterPlayer.CharacterJumpHeight

	elseif DButton == true then
		Right_Track:Play()

		local tweenInfo = TweenInfo.new(Right_Time, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
		local tween = TS:Create(BV, tweenInfo, {Velocity = hrp.CFrame.RightVector * Added_Distance})
		tween:Play()

		connection = RunService.RenderStepped:Connect(function()
			if not BV then
				connection:Disconnect()
			else
				BV.Velocity = hrp.CFrame.RightVector * Added_Distance
			end
		end)

		task.wait(Right_Time)
		BV:Destroy()
		connection:Disconnect()
		
		hum.WalkSpeed = StarterPlayer.CharacterWalkSpeed
		hum.JumpHeight = StarterPlayer.CharacterJumpHeight

	end
	
	task.wait(Cooldown)
	DashDebouce = false	
end


--//Getting the players direction
local function getMoveDirection()
	local rootPart = hum.RootPart
	local forwardDirection = rootPart.CFrame.LookVector
	local rightDirection = rootPart.CFrame.RightVector

	local moveDirection = forwardDirection * hum.MoveDirection.Z + rightDirection * hum.MoveDirection.X

	return moveDirection.Unit
end


--//Setting values
RunService.Heartbeat:Connect(function()
    local moveDirection = getMoveDirection()

    -- Reset buttons
    WButton, SButton, AButton, DButton = false, false, false, false

    if moveDirection.Magnitude > 0 then
        local dotForward = moveDirection:Dot(Vector3.new(0, 0, -1))
        local dotBackward = moveDirection:Dot(Vector3.new(0, 0, 1))
        local dotLeft = moveDirection:Dot(Vector3.new(-1, 0, 0))
        local dotRight = moveDirection:Dot(Vector3.new(1, 0, 0))

        if dotForward > 0.5 then
            SButton = true
        elseif dotBackward > 0.5 then
            WButton = true
        elseif dotLeft > 0.5 then
            AButton = true
        elseif dotRight > 0.5 then
            DButton = true
        end
    end
end)



--//Key pressing
uis.InputBegan:Connect(function(Input,IsTyping)
	if IsTyping then return end	
	
	if Input.KeyCode == Enum.KeyCode.Q and CheckingForValues() then
		Dashing()
	end
end)

The reason you probably don’t like the Dashing function is since as programmers we inherently learn to hate copy and pasting lines over and over again (because it makes it extremely difficult to edit later down the line). In the case of the Dashing function, it’s mostly copy and paste with just a bit of variation.

Comparing the differences between 2 of these if statements, you’ll note that there’s really not too much different:

Noting that, in this case it makes sense to use a general function which we can just pass through the track, time, and a number to be multiplied by the Added_Distance

-- Direction vector argument added since left and right is different from Front and Back
-- Same general idea though
local function DashDirection(track, duration, dirVectorName, directionMult, addedDistance)
	if addedDistance then
		Added_Distance += addedDistance -- For W and S
	end
	track:Play()

	local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	TS:Create(BV, tweenInfo, {Velocity = hrp.CFrame[dirVectorName] * directionMult * Added_Distance}):Play()
	-- You can one line, since you're not using the tween variable later

	connection = RunService.RenderStepped:Connect(function()
		if not BV then
			connection:Disconnect()
		else
			BV.Velocity = hrp.CFrame[dirVectorName] * -Added_Distance
		end
	end)

	task.wait(duration)
	BV:Destroy()
	connection:Disconnect()
end

With this generic function created, all that’s left is to use it.

local function Dashing()
	...

	if WButton then -- you also don't need == true. true == true -> true
		DashDirection(Front_Track, Front_Time, "LookVector", 1, 5)
	elseif SButton then
		DashDirection(Back_Track, Back_Time, "LookVector", -1, -10)
	elseif AButton then
		DashDirection(Left_Track, Left_Time, "RightVector", -1)
	elseif DButton then
		DashDirection(Right_Track, Right_Time, "RightVector", 1)
	end

	if WButton then -- Handle the delay outside of the dash function
		task.delay(.5,function() --kind of a uhh idk forgot the name but like you can't move for a bit after the front dash
			hum.WalkSpeed = StarterPlayer.CharacterWalkSpeed
			hum.JumpHeight = StarterPlayer.CharacterJumpHeight
		end)	
	else
		hum.WalkSpeed = StarterPlayer.CharacterWalkSpeed
		hum.JumpHeight = StarterPlayer.CharacterJumpHeight
	end

	task.wait(Cooldown)
	DashDebouce = false	
end

Outside of that, make sure that your syntax is consistent. Within your code you have abbreviations (TS, plr, hum, BV), you use camelCase sometimes, PascalCase sometimes, Snake_Case sometimes, with no consistency. This will make it harder to copy and paste code and in general abbreviations make it harder to come back and read code down the line.

-- also this
local function CheckingForValues()
	if hum.Health <= 0 or DashDebouce then return false else return true end
end
-- can become
local function CheckForValues()
    return not (hum.Health <= 0 or DashDebouce)
end
-- or this which is clearer than above probs
local function CheckForValues()
    return hum.Health > 0 and not DashDebounce
end

one last little thing but debounce is spelt incorrectly too

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.