Need help making ugly dash code pretty

Hi,

I made a dash module and I think it is very ugly in my opinion. I don’t know what it is but can anyone help me make it look better without just changing the casing? Or optimizing would be better. Thanks :wink:

local Dash = {}
local Blacklisted = {}

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local TweenService = game:GetService("TweenService")

local calculateRelativeDirection = require(ReplicatedStorage.Sources.Utility.calculateRelativeDirection)
local ContextActionUtility = require(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 Torso: Part = Character:WaitForChild("Torso")

local Animations = ReplicatedStorage.Assets.Animations.Movement

local DASH = false
local RAY_LENGTH = 15 -- in studs
local DASH_SPEED = 65
local COOLDOWN = 4

local ACTION_NAME = "Dash"
local IMAGE = "rbxassetid://16020771550"

local ANIMATIONS = {
	["Left"] = Animations.LeftDash,
	["Right"] = Animations.RightDash,
	["Backward"] = Animations.BackDash,
}

local UNDASHABLE_DIRECTIONS = {
	"Forward",
	"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

function Function(actionName, inputState, inputObject)
	local Direction = calculateRelativeDirection(workspace.CurrentCamera,Humanoid)
	
	if inputState ~= Enum.UserInputState.Begin then
		return
	end
	
	for _,v in pairs(UNDASHABLE_DIRECTIONS) do
		if v == Direction then
			return 
		end
	end
	
	if DASH then
		return
	end
	
	DASH = true
	-- dont remove these lines (they are important)
	Torso["Left Hip"].Enabled = true
	Torso["Right Hip"].Enabled = true
	
	local Animation = ANIMATIONS[Direction]
	local LoadedAnimation: AnimationTrack = Humanoid:FindFirstChild("Animator"):LoadAnimation(Animation)
	
	LoadedAnimation:Play()
	
	if Direction == "Left" then
		local Tween = TweenService:Create(Root,TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Velocity = Root.CFrame.RightVector * -DASH_SPEED})
		Tween:Play()
	elseif Direction == "Right" then
		local Tween = TweenService:Create(Root,TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Velocity = Root.CFrame.RightVector * DASH_SPEED})
		Tween:Play()
	elseif Direction == "Backward" then
		local Tween = TweenService:Create(Root,TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{Velocity = Root.CFrame.LookVector * -DASH_SPEED})
		Tween:Play()
	end

	local Stopped = LoadedAnimation.Stopped:Connect(function() 
		Torso["Left Hip"].Enabled = false
		Torso["Right Hip"].Enabled = false
	end)
	
	local Sound = CreateSound(Root)
	Sound:Play()
	
	Debris:AddItem(Sound, 2)
	
	task.wait(COOLDOWN)
	
	Stopped:Disconnect()
	
	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
2 Likes

This looks pretty clean already, there are a few things you can do though.

  1. Reuse TweenInfos
  2. Use table.find instead of a loop.
  3. Move around some code

Code:

local Dash = {}
local Blacklisted = {}

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local TweenService = game:GetService("TweenService")

local calculateRelativeDirection = require(ReplicatedStorage.Sources.Utility.calculateRelativeDirection)
local ContextActionUtility = require(ReplicatedStorage.Sources.Utility.ContextActionUtility)

local Player = Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local Torso = Character:WaitForChild("Torso")
local LeftHip = Torso:WaitForChild("Left Hip")
local RightHip = Torso:WaitForChild("Right Hip")

local Animations = ReplicatedStorage.Assets.Animations.Movement
local currentCamera = workspace.CurrentCamera

local DASH = false
local RAY_LENGTH = 15 -- in studs
local DASH_SPEED = 65
local COOLDOWN = 4
local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Linear)

local ACTION_NAME = "Dash"
local IMAGE = "rbxassetid://16020771550"

local ANIMATIONS = {
	Left = Animations.LeftDash,
	Right = Animations.RightDash,
	Backward = Animations.BackDash,
}

local UNDASHABLE_DIRECTIONS = {
	"Forward",
	"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
	
	Debris:AddItem(Sound, 2)
	
	return Sound
end

function Function(actionName, inputState, inputObject)
	if inputState ~= Enum.UserInputState.Begin or DASH then
		return
	end
	
	local Direction = calculateRelativeDirection(currentCamera, Humanoid)
	
	if table.find(UNDASHABLE_DIRECTIONS, Direction) then
		return
	end

	DASH = true
	
	-- dont remove these lines (they are important)
	LeftHip.Enabled = true
	RightHip.Enabled = true

	local Animation = ANIMATIONS[Direction]
	
	local LoadedAnimation: AnimationTrack = Humanoid.Animator:LoadAnimation(Animation)
	LoadedAnimation:Play()

	if Direction == "Left" then
		local Tween = TweenService:Create(Root, tweenInfo, {Velocity = Root.CFrame.RightVector * -DASH_SPEED})
		Tween:Play()
	elseif Direction == "Right" then
		local Tween = TweenService:Create(Root, tweenInfo, {Velocity = Root.CFrame.RightVector * DASH_SPEED})
		Tween:Play()
	elseif Direction == "Backward" then
		local Tween = TweenService:Create(Root, tweenInfo, {Velocity = Root.CFrame.LookVector * -DASH_SPEED})
		Tween:Play()
	end

	local Stopped = LoadedAnimation.Stopped:Connect(function() 
		LeftHip.Enabled = false
		RightHip.Enabled = false
	end)

	local Sound = CreateSound(Root)
	Sound:Play()

	task.wait(COOLDOWN)

	Stopped:Disconnect()
	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
2 Likes

Thanks but I always feel like there’s room for improvement.

The table.find really helps in this case. Thank you!!!

1 Like

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