Problems making a Dash system (referrence is the strongest battlegrounds)

Hey everyone, I’m trying to make a battlegrounds game and I am working on the dashing right now. I want to make it kinda like the Strongest Battlegrounds where it dashes then punches after pressing Q. The dash part of it works, but the punching doesnt every time. It is kind of random. Also the hitbox stays behind instead of infront of the character.

robloxapp-20240812-1654121.wmv (4.3 MB)

I have tried messing with it a bit but not much has worked.

Local Script:

local UserInputService = game:GetService("UserInputService")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local debounce = false
local dashDistance = 20
local dashDuration = 0.2
local dashCooldown = 2
local dashSpeed = dashDistance / dashDuration

local forwardAnimation = script.Dash

local function performDash(direction)
	if debounce then return end
	debounce = true

	local dashVelocity = Vector3.new(0, 0, 0)
	if direction == "Forward" then
		for i,v in pairs(humanoid:GetPlayingAnimationTracks()) do
			v:Stop()
		end
		local loadedForwardDash = humanoid.Animator:LoadAnimation(forwardAnimation)
		loadedForwardDash:Play()
		loadedForwardDash:GetMarkerReachedSignal("Dash"):Connect(function()
			loadedForwardDash:AdjustSpeed(0)
		end)
		dashVelocity = character.PrimaryPart.CFrame.LookVector * dashSpeed
		wait(.1)
		loadedForwardDash:AdjustSpeed(1)
		loadedForwardDash:GetMarkerReachedSignal("Punch"):Connect(function()
			ReplicatedStorage.Events.DashPunch:FireServer()
		end)
	elseif direction == "Backward" then
		dashVelocity = -character.PrimaryPart.CFrame.LookVector * dashSpeed
	elseif direction == "Left" then
		dashVelocity = -character.PrimaryPart.CFrame.RightVector * dashSpeed
	elseif direction == "Right" then
		dashVelocity = character.PrimaryPart.CFrame.RightVector * dashSpeed
	end

	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.Velocity = dashVelocity
	bodyVelocity.MaxForce = Vector3.new(1e5, 0, 1e5)
	bodyVelocity.P = 9e4

	bodyVelocity.Parent = character.PrimaryPart

	wait(dashDuration)

	bodyVelocity:Destroy()

	wait(dashCooldown)
	debounce = false
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.Q then
		local direction = "Forward"
		if UserInputService:IsKeyDown(Enum.KeyCode.S) then
			direction = "Backward"
		elseif UserInputService:IsKeyDown(Enum.KeyCode.A) then
			direction = "Left"
		elseif UserInputService:IsKeyDown(Enum.KeyCode.D) then
			direction = "Right"
		end
		performDash(direction)
	end
end)

Server Script:

local AttackHitboxes = require(script.Parent.Services.AttackHitboxes)

game:GetService("ReplicatedStorage").Events.DashPunch.OnServerEvent:Connect(function(Player)
	local character = Player.Character
	AttackHitboxes.CreateHitboxes(character, Vector3.new(9, 9, 5), 10, true)
end)

The hitbox script is a whole other module script… same one used for the fighting system that I have.

1 Like

You should change your code to this :

local function performDash(direction)
	if debounce then return end
	
	debounce = true
	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.MaxForce = Vector3.new(1e5, 0, 1e5)
	bodyVelocity.P = 9e4

	bodyVelocity.Parent = character.PrimaryPart
	local dashVelocity = Vector3.new(0, 0, 0)
	bodyVelocity.Velocity = dashVelocity
	
	if direction == "Forward" then
		for i,v in pairs(humanoid:GetPlayingAnimationTracks()) do
			v:Stop()
		end
		local loadedForwardDash = humanoid.Animator:LoadAnimation(forwardAnimation)
		loadedForwardDash:Play()
		loadedForwardDash:GetMarkerReachedSignal("Dash"):Connect(function()
			loadedForwardDash:AdjustSpeed(0)
			wait(dashTime)
			loadedForwardDash:AdjustSpeed(1)
		end)
		loadedForwardDash:GetMarkerReachedSignal("Punch"):Connect(function()
			ReplicatedStorage.Events.DashPunch:FireServer()
			bodyVelocity:Destroy()
		end)
	elseif direction == "Backward" then
		dashVelocity = -character.PrimaryPart.CFrame.LookVector * dashSpeed
		bodyVelocity.Velocity = dashVelocity
		wait(dashTime)
		bodyVelocity:Destroy()
	elseif direction == "Left" then
		dashVelocity = -character.PrimaryPart.CFrame.RightVector * dashSpeed
		bodyVelocity.Velocity = dashVelocity
		wait(dashTime)
		bodyVelocity:Destroy()
	elseif direction == "Right" then
		dashVelocity = character.PrimaryPart.CFrame.RightVector * dashSpeed
		bodyVelocity.Velocity = dashVelocity
		wait(dashTime)
		bodyVelocity:Destroy()
	end

	wait(dashCooldown)
	debounce = false
end

make the body velocity at the top of the script

If this still doesnt work check that you have published the correct animation with all events, if that also doesnt work use keyframe reached instead of get marker reached signal as it works better.

2 Likes

Real quick, what do I set dashVelocity to? would it just be dashDistance?

1 Like

You set bodyVelocity.Velocity = dashVelocity every time you change dashVelocity Ill add that in rq

1 Like

Ahh okay because i see it is set to 0,0,0 and it is defined below the line that its set to so i was a little confused lol. mb.

Hey sorry to bother still, this works but I don’t move anywhere. How could I fix that?

Sorry, mistake on my part again, you should set dashVelocity at the top of the script to :
BodyVelocity.Velocity = character.PrimaryPart.CFrame.LookVector * dashSpeed instead of Vector3.new(0,0,0)

1 Like

As in like I set that to the dashVelocity variable or replace the line under the dashVelocity variable?

Replace this :

bodyVelocity.Parent = character.PrimaryPart
local dashVelocity = Vector3.new(0, 0, 0)
bodyVelocity.Velocity = dashVelocity

with this :

bodyVelocity.Parent = character.PrimaryPart
local dashVelocity = character.PrimaryPart.CFrame.LookVector * dashSpeed
bodyVelocity.Velocity = dashVelocity
1 Like

Okay thanks. It works but doesnt destroy the velocity or anything and it keeps pushing me forward no matter what.

Make sure your punch marker is being triggered you can test that by adding print inside of its function, dont forget to add bodyVelocity:Destroy() inside it as well

1 Like

I found the problem after some time, it creates 2 bodyVelocitys after clicking Q once and deletes one of the bodyVelocitys, leaving the other one there.

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