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