My script for a dash is good?

Here a script!

LOCAL:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local rootpart = script.Parent.Parent:WaitForChild("HumanoidRootPart")
local animationController = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid"):WaitForChild("Animator")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

--dash
local dashevent = game:GetService("ReplicatedStorage").Events.Dash
local animation = Instance.new("Animation")
--end
local animations = {
	["DashFront"] = "rbxassetid://16566527703";
	["DashLeft"] = "rbxassetid://16591663577";
	["DashRight"] = "rbxassetid://16596178032";
	["DashDown"] = "rbxassetid://16613175493";
}

local dashreload = false
local sidedashreload = false
local sidedashplaying = false

UIS.InputBegan:Connect(function(key, typing)
	local humanoid = char:WaitForChild("Humanoid")
	if humanoid.WalkSpeed == 0 or humanoid.WalkSpeed == 2 then return end
	local dashanim = "Front"
	if typing then return end
	if key.KeyCode == Enum.KeyCode.Q then	
		local DHold = UIS:IsKeyDown(Enum.KeyCode.D)
		local WHold = UIS:IsKeyDown(Enum.KeyCode.W)
		local SHeld = UIS:IsKeyDown(Enum.KeyCode.S)
		local AHeld = UIS:IsKeyDown(Enum.KeyCode.A)

		if WHold == true then
			dashanim = "Front"
		end

		if AHeld == true then
			dashanim = "Left"
		end

		if DHold == true then
			dashanim = "Right"
		end

		if SHeld == true then
			dashanim = "Down"
		end

	animation.AnimationId = animations["Dash"..dashanim]
	local dashanimationTrack = animationController:LoadAnimation(animation)
		
		humanoid.WalkSpeed = 0
		local Attachment = Instance.new("Attachment", rootpart)
		local BV = Instance.new("BodyVelocity", Attachment)
		if dashanim == "Front" then
			if sidedashplaying == true then return end
			if dashreload == true then humanoid.WalkSpeed = 21; Attachment:Destroy(); dashanimationTrack:Stop() return end
			local playing = false
			dashreload = true
			BV.MaxForce = Vector3.new(math.huge,0,math.huge)
			BV.P = 9e9
			BV.Parent = rootpart
			playing = true

			local function velocity()
				local connection
				connection = RunService.Heartbeat:Connect(function()
					BV.Velocity = rootpart.CFrame.LookVector * 1 * 55
					if not playing then
						connection:Disconnect()
					end
				end)
			end

			velocity()
			task.wait(0.01)
			dashanimationTrack:Play()
			dashevent:FireServer(player)
			dashevent.OnClientEvent:Connect(function(player, results)
				BV:Destroy()
				playing = false
				Attachment:Destroy()
				humanoid.WalkSpeed = 21
			wait(5.5)
			dashreload = false
			end)
		elseif dashanim == "Left" then
			local playing = false
			sidedashplaying = true
			if sidedashreload == true then humanoid.WalkSpeed = 21 Attachment:Destroy() dashanimationTrack:Stop() return end
			sidedashreload = true
			BV.MaxForce = Vector3.new(math.huge,0,math.huge)
			BV.P = 8e8
			BV.Parent = rootpart
			playing = true

			local function velocity()
				local connection
				connection = RunService.Heartbeat:Connect(function()
					BV.Velocity = rootpart.CFrame.LookVector * (rootpart.CFrame.RightVector * -1) * 65
					if not playing then
						connection:Disconnect()
					end
				end)
			end

			velocity()

			dashanimationTrack:Play()
			task.wait(0.3)
				BV:Destroy()
				Attachment:Destroy()
				sidedashplaying = false
				humanoid.WalkSpeed = 21
			wait(3)
			if sidedashreload ~= false then
				sidedashreload = false
			end
		elseif dashanim == "Right" then
			sidedashplaying = true
			local playing = false
			if sidedashreload == true then humanoid.WalkSpeed = 21 Attachment:Destroy() dashanimationTrack:Stop() return end
			sidedashreload = true
			BV.MaxForce = Vector3.new(math.huge,0,math.huge)
			BV.P = 8e8
			BV.Parent = rootpart
			playing = true

			local function velocity()
				local connection
				connection = RunService.Heartbeat:Connect(function()
					BV.Velocity = rootpart.CFrame.RightVector * 1 * 65
					if not playing then
						connection:Disconnect()
					end
				end)
			end
			velocity()

			dashanimationTrack:Play()
			task.wait(0.3)
				playing = false
				BV:Destroy()
				Attachment:Destroy()
				sidedashplaying = false
				humanoid.WalkSpeed = 21
			wait(3)
			if sidedashreload ~= false then
				sidedashreload = false
			end
		elseif dashanim == "Down" then
			local playing = false
			if dashreload == true then Attachment:Destroy() humanoid.WalkSpeed = 21 dashanimationTrack:Stop() return end
			dashreload = true
			BV.MaxForce = Vector3.new(math.huge,0,math.huge)
			BV.P = 8e8
			BV.Parent = rootpart
			playing = true

			local function velocity()
				local connection
				connection = RunService.Heartbeat:Connect(function()
					BV.Velocity = rootpart.CFrame.LookVector * -1 * 55
					if not playing then
						connection:Disconnect()
					end
				end)
			end
			velocity()

			dashanimationTrack:Play()
			task.wait(0.3)
			playing = false
			BV:Destroy()
			Attachment:Destroy()
			humanoid.WalkSpeed = 21
			wait(3)
			if sidedashreload ~= false then
				sidedashreload = false
			end
			dashreload = false
		end
	end
end)

SERVER (Used for a front dash hitbox):

local dashevent = game:GetService("ReplicatedStorage").Events.Dash
local stun = require(game.ServerStorage.Module.StunHandlerV2)
local player = game.Players.LocalPlayer

--end
local hitanimations = {
	["Dash1"] = game.ReplicatedStorage["Hitted combo"].Dash1;
}

dashevent.OnServerEvent:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local rootpart = char:WaitForChild("HumanoidRootPart")
	local hitbox = Instance.new("Part")
	hitbox.Size = Vector3.new(3,3,3)
	hitbox.Parent = rootpart
	hitbox.CanCollide = false
	hitbox.CFrame = rootpart.CFrame + (rootpart.CFrame.lookVector * 3.5) 
	hitbox.Transparency = 0
	local weld = Instance.new("WeldConstraint", hitbox)
	weld.Part0 = hitbox
	weld.Part1 = hitbox.Parent
	hitbox.Name = "Hitbox"
	local humanoid = char:WaitForChild("Humanoid")
	local results = nil
	local checkcorrect = false

	local blockresults = "bad"

	local function check()
		if hitbox.CanTouch == false then return end
		hitbox.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then

				local function hitted()
					hitbox.CanTouch = false
					local numberhit = 1
					local enemy = hit.Parent:WaitForChild("HumanoidRootPart")
					local enehum = hit.Parent:WaitForChild("Humanoid")
					local animator = enehum.Animator
					local animation = hitanimations["Dash"..numberhit]
					local dashanimationTrack = animator:LoadAnimation(animation)
					if animator then
						dashanimationTrack:Play()
					end
					enehum:TakeDamage(4)
					stun.Stun(enehum, 2)
					checkcorrect = true
					dashevent:FireClient(player, results)
				end

				if not rootpart:FindFirstChild("BlockHitbox") then hitted() 
				else
					for v, part in pairs(rootpart.BlockHitbox) do
						if part.Name == "Hitbox" then end
					end
				end
				
			end
		end)
	end

		repeat check() until hitbox.CanTouch == true or results == true

	task.wait(0.6)
	results = false
	if results == false and checkcorrect ~= true then
		dashevent:FireClient(player, results)
	end
	hitbox:Destroy()
	humanoid.WalkSpeed = 21
end)

Is this everything is good?

5 Likes

Does it work if so then it’s good :+1: functionally over style it looks fine. It’s not really the style I write code but I don’t see any big hiccups or lag causers in the code

4 Likes

BodyVelocity is deprecated instead use LinearVelocity

i know but he has some bad stuffs like u are not falling when using a dash :frowning:

Looked at the client script, you have a lot of code within the if statements that is the same in all of them. I’d recommend removing those out of the if statement as it would overall reduce the amount of code you have (cleaning it up a bunch in the process)

There’s also redundant variables like playing, which you can just remove and disconnect the connection directly after the slide is done.

So theoretically, something like this should be able to replace those if statements which will cut down on the total amount of code.

		humanoid.WalkSpeed = 0
		local Attachment = Instance.new("Attachment", rootpart)
		local BV = Instance.new("BodyVelocity", Attachment)

		if dashanim == "Front" and sidedashplaying then return end
		if (dashanim == "Front" or dashanim == "Down") and dashreload then humanoid.WalkSpeed = 21; Attachment:Destroy(); dashanimationTrack:Stop() return end
		if (dashanim == "Left" or dashanim == "Right") and sidedashreload then humanoid.WalkSpeed = 21 Attachment:Destroy() dashanimationTrack:Stop() return end

		if (dashanim == "Front" or dashanim == "Down") then
			dashreload = true
		else
			sidedashreload = true
		end
		BV.MaxForce = Vector3.new(math.huge,0,math.huge)
		BV.P = dashanim == "Front" and 9e9 or 8e8
		BV.Parent = rootpart

		local connection = RunService.Heartbeat:Connect(function()
			if dashanim == "Front" then
				BV.Velocity = rootpart.CFrame.RightVector * 1 * 65
			elseif dashanim == "Left" then
				BV.Velocity = rootpart.CFrame.LookVector * (rootpart.CFrame.RightVector * -1) * 65
			elseif dashanim == "Right" then
				BV.Velocity = rootpart.CFrame.RightVector * 1 * 65
			elseif dashanim == "Down" then
				BV.Velocity = rootpart.CFrame.LookVector * -1 * 55
			end
		end)

		dashanimationTrack:Play()
		task.wait(0.3)
		BV:Destroy()
		Attachment:Destroy()
		sidedashplaying = false
		humanoid.WalkSpeed = 21
		connection:Disconnect()
		wait(3)
		if sidedashreload ~= false then
			sidedashreload = false
		end
		if (dashanim == "Front" or dashanim == "Down") then
			dashreload = false
		end
1 Like

Just a random question… Why is there a hitbox thingy for the dash script for damaging? I originally thought it was a thing where if a player dashes onto a wall it’ll cancel their dash but it seems to damage other humanoids. Also, should use a better hit detector for example like the one I had

Well, I’d not use .Touched for my hitboxes, I’d use :GetPartBoundsInBox() instead.

1 Like

just dash like the strongest battlegrounds. Its a reason.

The dash in strongest doesn’t use hitbox. Especially one that damages people.

Actually, he is using it for damage, stun and animation.

Just because it’s deprecated doesn’t mean it’s bad, I’ve used it for a while and never had issues with it, even after loading old games, it worked, and worked really good might I add.

1 Like