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)