Repeated lines in dash script

Hey,

I made a dash script and I need help optimizing it. The dash function reuses a lot of the same lines which I don’t like… The lines are only changed by a tiny bit.

Dash:

local Dash = {}
local Blacklisted = {}

local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
RaycastParameters.FilterDescendantsInstances = Blacklisted

local calculateRelativeDirection = require(game:GetService("ReplicatedStorage").Sources.Utility.calculateRelativeDirection)
local ContextActionUtility = require(game:GetService("ReplicatedStorage").Sources.Utility.ContextActionUtility)

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Root: Part = Character:WaitForChild("HumanoidRootPart")

local DEBUG_MODE = false
local RAY_LENGTH = 15 -- in studs
local ACTION_NAME = "Dash"
local IMAGE = "rbxassetid://16020771550"
local COOLDOWN = 4
local DASH = false

local UNDASHABLE_DIRECTIONS = {
	"ForwardLeft",
	"ForwardRight",
	"BackwardLeft",
	"BackwardRight";
}

local function CreateSound(Root)
	local Sound = Instance.new("Sound")
	Sound.Name = "Dash"
	Sound.SoundId = "rbxassetid://6128977275"
	Sound.Volume = 1.5
	
	Sound.RollOffMaxDistance = 90
	Sound.RollOffMinDistance = 4
	
	Sound.Parent = Root
	return Sound
end

-- rays
local function LeftWallCheck()
	return workspace:Raycast(Root.Position, -Root.CFrame.RightVector * RAY_LENGTH, RaycastParameters)
end

local function RightWallCheck()
	return workspace:Raycast(Root.Position, Root.CFrame.RightVector * RAY_LENGTH, RaycastParameters)
end

local function BackWallCheck()
	return workspace:Raycast(Root.Position, -Root.CFrame.LookVector * RAY_LENGTH, RaycastParameters)
end
-- debug func
function VisibleRay(Result, RayType: string)
	if not DEBUG_MODE then return end

	local Distance = (Root.Position - Result.Position).Magnitude
	local Part = Instance.new("Part")
	Part.Anchored = true
	Part.CanCollide = false
	Part.Size = Vector3.new(0.1, 0.1, Distance)
	Part.CFrame = CFrame.lookAt(Root.Position, Result.Position)*CFrame.new(0, 0, -Distance/2)
	Part.Color = Color3.new(1,0,0)
	Part.Parent = workspace.Terrain.Rays
	Part.Name = RayType


	task.delay(5, function()
		Part:Destroy()
	end)
end


function Function(actionName, inputState, inputObject)
	local Direction = calculateRelativeDirection(workspace.CurrentCamera,Humanoid)
	
	if inputState ~= Enum.UserInputState.Begin then
		return
	end
	
	if Direction[UNDASHABLE_DIRECTIONS] then
		return
	end
	
	if Dash == true then
		return
	end
	
	Dash = true
	
	if Direction == "Backward" then
		local BackRay = BackWallCheck()
		
		local LookVector = Root.CFrame.LookVector 
		local UnitVector = LookVector.Unit 
		local OffsetVector = UnitVector * RAY_LENGTH
		
		if BackRay then
			local newLength = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - BackRay.Position).Magnitude - 1
			local OffsetVector = UnitVector * newLength
		end
		
		Character:SetPrimaryPartCFrame(CFrame.new(Character.HumanoidRootPart.Position - OffsetVector))
	end

	if Direction == "Left" then
		local LeftRay = LeftWallCheck()

		local LookVector = Root.CFrame.LookVector 
		local UnitVector = LookVector.Unit 
		local OffsetVector = UnitVector * RAY_LENGTH

		if LeftRay then
			local newLength = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - LeftRay.Position).Magnitude - 1
			local OffsetVector = UnitVector * newLength
		end

		Character:SetPrimaryPartCFrame(CFrame.new(Character.HumanoidRootPart.Position - OffsetVector))
	end

	if Direction == "Right" then
		local RightRay = RightWallCheck()

		local LookVector = Root.CFrame.LookVector 
		local UnitVector = LookVector.Unit 
		local OffsetVector = UnitVector * RAY_LENGTH

		if RightRay then
			local newLength = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - RightRay.Position).Magnitude - 1
			local OffsetVector = UnitVector * newLength
		end

		Character:SetPrimaryPartCFrame(CFrame.new(Character.HumanoidRootPart.Position - OffsetVector))
	end
	
	local Sound = CreateSound(Root)
	Sound:Play()
	
	game.Debris:AddItem(Sound, 2)
	
	task.wait(COOLDOWN)
	Dash = false
end

function Dash.Bind(boolean)
	if boolean then
		ContextActionUtility:BindAction(ACTION_NAME,Function,true,Enum.KeyCode.Q)
		ContextActionUtility:SetImage(IMAGE)
	else
		ContextActionUtility:UnbindAction(ACTION_NAME)
	end
end

return Dash

1 Like
local Dash = {}
local Blacklisted = {}

local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
RaycastParameters.FilterDescendantsInstances = Blacklisted

local calculateRelativeDirection = require(game:GetService("ReplicatedStorage").Sources.Utility.calculateRelativeDirection)
local ContextActionUtility = require(game:GetService("ReplicatedStorage").Sources.Utility.ContextActionUtility)

local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")

local DEBUG_MODE = false
local RAY_LENGTH = 15
local ACTION_NAME = "Dash"
local IMAGE = "rbxassetid://16020771550"
local COOLDOWN = 4
local DASH = false

local UNDASHABLE_DIRECTIONS = {
    ForwardLeft = true,
    ForwardRight = true,
    BackwardLeft = true,
    BackwardRight = true
}

local function CreateSound()
    local Sound = Instance.new("Sound")
    Sound.Name = "Dash"
    Sound.SoundId = "rbxassetid://6128977275"
    Sound.Volume = 1.5
    Sound.RollOffMaxDistance = 90
    Sound.RollOffMinDistance = 4
    Sound.Parent = Root
    return Sound
end

local function WallCheck(direction)
    return workspace:Raycast(Root.Position, Root.CFrame[direction .. "Vector"] * (direction:find("Back") and -RAY_LENGTH or RAY_LENGTH), RaycastParameters)
end

local function VisibleRay(result, rayType)
    if not DEBUG_MODE then return end
    local distance = (Root.Position - result.Position).Magnitude
    local part = Instance.new("Part")
    part.Anchored, part.CanCollide = true, false
    part.Size = Vector3.new(0.1, 0.1, distance)
    part.CFrame = CFrame.lookAt(Root.Position, result.Position) * CFrame.new(0, 0, -distance / 2)
    part.Color = Color3.new(1, 0, 0)
    part.Parent = workspace.Terrain.Rays
    part.Name = rayType
    task.delay(5, function() part:Destroy() end)
end

local function DashAction(inputState, inputObject)
    if inputState ~= Enum.UserInputState.Begin or DASH or UNDASHABLE_DIRECTIONS[calculateRelativeDirection(workspace.CurrentCamera, Humanoid)] then
        return
    end
    
    DASH = true
    
    local direction = calculateRelativeDirection(workspace.CurrentCamera, Humanoid)
    local ray = WallCheck(direction)

    local lookVector = Root.CFrame.LookVector
    local unitVector = lookVector.Unit
    local offsetVector = unitVector * RAY_LENGTH

    if ray then
        local newLength = (Character.HumanoidRootPart.Position - ray.Position).Magnitude - 1
        offsetVector = unitVector * newLength
    end

    Character:SetPrimaryPartCFrame(CFrame.new(Character.HumanoidRootPart.Position - offsetVector))
    
    local sound = CreateSound()
    sound:Play()
    game.Debris:AddItem(sound, 2)
    
    task.wait(COOLDOWN)
    DASH = false
end

function Dash.Bind(boolean)
    if boolean then
        ContextActionUtility:BindAction(ACTION_NAME, DashAction, true, Enum.KeyCode.Q)
        ContextActionUtility:SetImage(IMAGE)
    else
        ContextActionUtility:UnbindAction(ACTION_NAME)
    end
end

return Dash

Cleaned up the code a bit