Jump Not Canceling Script?

So pretty much I tried to make it so if you press space it cancels the dashing, not diving, part of the script. However, this does not work. And I’m just wondering why. It does a little hop, maybe less than a stud, but it neither cancels the script or jumps, which it should. I’m wondering if anyone has any ideas on how to fix this bug, thanks.

This is a LocalScript in StarterCharacterScripts:

-- Get Necessary Services --
local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local VfxFolder = RS:WaitForChild("VfxFolder")
local DashClone = VfxFolder:WaitForChild("DashClone")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

-- Get Local Player and Character --
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()

-- Get References to Humanoid and Root Part --
local Hum = Char:WaitForChild("Humanoid")
local HRP = Char:WaitForChild("HumanoidRootPart")

-- Track Key States --
local WKeyDown = false
local AKeyDown = false
local SKeyDown = false
local DKeyDown = false

-- Manage Dashing --
local DashDeb = false
local DashCooldownTime = 2 -- Cooldown time for dashing
local DashTime = 0.5
local DivingDeb = false
local DiveCooldownTime = 2 -- Cooldown time for diving
local DivingTime = 0.5
local CanDashAfterDive = false -- Flag to track if dashing is allowed after a dive

-- Track cooldown state between rolling and diving
local RollDiveCooldown = false
local RollDiveCooldownTime = 1 -- 1-second cooldown

-- Load Dash Animations --
local AnimationFolder = script:WaitForChild("AnimationFolder")
local ForwardDashAnime = Hum:LoadAnimation(AnimationFolder:WaitForChild("ForwardDash"))
local BackDashAnime = Hum:LoadAnimation(AnimationFolder:WaitForChild("BackDash"))
local LeftDashAnime = Hum:LoadAnimation(AnimationFolder:WaitForChild("LeftDash"))
local RightDashAnime = Hum:LoadAnimation(AnimationFolder:WaitForChild("RightDash"))
local DiveAnim = Instance.new("Animation")
DiveAnim.AnimationId = "rbxassetid://76120423649768"

-- Event for Dashing --
local Event = script:WaitForChild("Event")

-- Function to check if the player is in the air
local function isInAir()
	return Char.Humanoid.FloorMaterial == Enum.Material.Air
end

-- Check if the player is swimming (based on the Swimming attribute from the Swim script)
local function isSwimming()
	return Char:GetAttribute("Swimming") == true
end

-- Create Dash Effect --
function DashEffect()
	for i = 1, 7 do
		local Clone = DashClone:Clone()
		Clone:SetPrimaryPartCFrame(HRP.CFrame)
		Clone.Parent = workspace
		game.Debris:AddItem(Clone, 0.5)

		spawn(function()
			for _, v in pairs(Clone:GetChildren()) do
				spawn(function()
					if v:IsA("MeshPart") or v:IsA("Part") then
						v.CFrame = Char:FindFirstChild(v.Name).CFrame
						for t = 0.25, 1, 0.1 do
							v.Transparency = t
							v.Reflectance = t
							wait()
						end
						Clone:Destroy()
					end
				end)
			end
		end)

		wait(0.05)
	end
end

-- End Dash Function --
local function endDash(tween, animation)
	if tween then tween:Cancel() end
	if animation then animation:Stop() end
	DashDeb = true -- Trigger cooldown
	task.delay(DashCooldownTime, function()
		DashDeb = false
	end)
end

-- Handle Input for Dashing and Diving --
UIS.InputBegan:Connect(function(Key, IsTyping)
	if IsTyping then return end

	-- Cancel dash when pressing C
	if Key.KeyCode == Enum.KeyCode.C and DashDeb == true then
		-- Stop any currently playing dash animations
		ForwardDashAnime:Stop()
		LeftDashAnime:Stop()
		BackDashAnime:Stop()
		RightDashAnime:Stop()
		DashDeb = false
		print("Dash canceled")
	end

	-- Check for Dive first, if in the air and not swimming, with the cooldown check
	if Key.KeyCode == Enum.KeyCode.R then
		if not DivingDeb and isInAir() and not isSwimming() and not RollDiveCooldown then
			DivingDeb = true
			local playAnim = Char.Humanoid:LoadAnimation(DiveAnim)
			playAnim:Play()

			-- Create BodyVelocity for diving with an arc --
			local dive = Instance.new("BodyVelocity")
			dive.MaxForce = Vector3.new(50000, 50000, 50000)

			-- Apply initial upward velocity to create an arc, then downward --
			local diveVelocity = HRP.CFrame.LookVector * 45 + Vector3.new(0, 20, 0)  -- Increased horizontal force by 50%
			dive.Velocity = diveVelocity
			dive.Parent = HRP

			-- Flag to detect if the player hits a wall
			local hitWall = false

			-- Raycast Logic to Detect Collisions --
			RunService.Heartbeat:Connect(function()
				if dive and not hitWall then
					-- Calculate the direction in which the player is moving
					local forwardDirection = HRP.CFrame.LookVector
					local rayDirection = forwardDirection * 3 -- Check in front of the player

					-- Create RaycastParams
					local rayParams = RaycastParams.new()
					rayParams.FilterDescendantsInstances = {Char} -- Exclude player's character
					rayParams.FilterType = Enum.RaycastFilterType.Exclude

					-- Perform the raycast
					local raycastResult = workspace:Raycast(HRP.Position + Vector3.new(0, 2, 0), rayDirection, rayParams)

					-- If the ray hits a wall
					if raycastResult and raycastResult.Instance and raycastResult.Instance.CanCollide then
						-- Cancel the dive and stop the animation
						dive:Destroy()
						playAnim:Stop()
						hitWall = true
					end
				end
			end)

			-- Keep applying force until the player reaches the ground or starts swimming
			while isInAir() do
				-- Stop diving if swimming
				if isSwimming() then
					playAnim:Stop()
					dive:Destroy()
					break
				end

				-- Cancel dive if Space or T is pressed
				if UIS:IsKeyDown(Enum.KeyCode.Space) or UIS:IsKeyDown(Enum.KeyCode.T) then
					playAnim:Stop()
					dive:Destroy()
					break
				end

				dive.Velocity = dive.Velocity + Vector3.new(0, -5, 0)  -- Gradual downward force
				wait(0.1)
			end

			-- Play ForwardDash animation upon landing
			if not isInAir() then
				playAnim:Stop()  -- Stop the dive animation
				ForwardDashAnime:Play()  -- Play the ForwardDash animation
			end

			-- Stop diving and clean up
			playAnim:Stop()
			dive:Destroy()

			-- Set cooldown to allow dash after diving
			CanDashAfterDive = true
			task.delay(0.5, function()
				CanDashAfterDive = false
			end)

			task.delay(DiveCooldownTime, function()
				DivingDeb = false
			end)

			-- Start the cooldown for RollDiveCooldown
			RollDiveCooldown = true
			task.delay(RollDiveCooldownTime, function()
				RollDiveCooldown = false
			end)

		elseif DashDeb == false and not isInAir() and Char:FindFirstChild("Deb") == nil and not CanDashAfterDive then
			-- Dash Logic --
			DashDeb = true
			task.delay(DashCooldownTime, function()
				DashDeb = false
			end)

			-- Dash Forward --
			if WKeyDown then
				if isInAir() then return end -- Cancel dash if in the air
				coroutine.wrap(DashEffect)()
				local Tween = TweenService:Create(HRP, TweenInfo.new(DashTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Velocity = HRP.CFrame.LookVector * 100})
				Tween:Play()
				ForwardDashAnime:Play()
				Event:FireServer("Dash")

				delay(DashTime + 0.1, function()
					ForwardDashAnime:Stop()
				end)

				-- Dash Left --
			elseif AKeyDown then
				if isInAir() then return end -- Cancel dash if in the air
				coroutine.wrap(DashEffect)()
				local Tween = TweenService:Create(HRP, TweenInfo.new(DashTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Velocity = HRP.CFrame.RightVector * -100})
				Tween:Play()
				LeftDashAnime:Play()
				Event:FireServer("Dash")

				delay(DashTime + 0.1, function()
					LeftDashAnime:Stop()
				end)

				-- Dash Backward --
			elseif SKeyDown then
				if isInAir() then return end -- Cancel dash if in the air
				coroutine.wrap(DashEffect)()
				local Tween = TweenService:Create(HRP, TweenInfo.new(DashTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Velocity = HRP.CFrame.LookVector * -100})
				Tween:Play()
				BackDashAnime:Play()
				Event:FireServer("Dash")

				delay(DashTime + 0.1, function()
					BackDashAnime:Stop()
				end)

				-- Dash Right --
			elseif DKeyDown then
				if isInAir() then return end -- Cancel dash if in the air
				coroutine.wrap(DashEffect)()
				local Tween = TweenService:Create(HRP, TweenInfo.new(DashTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Velocity = HRP.CFrame.RightVector * 100})
				Tween:Play()
				RightDashAnime:Play()
				Event:FireServer("Dash")

				delay(DashTime + 0.1, function()
					RightDashAnime:Stop()
				end)
			end
		end
	end
end)

-- Track Key States Continuously --
RunService.RenderStepped:Connect(function()
	WKeyDown = UIS:IsKeyDown(Enum.KeyCode.W)
	AKeyDown = UIS:IsKeyDown(Enum.KeyCode.A)
	SKeyDown = UIS:IsKeyDown(Enum.KeyCode.S)
	DKeyDown = UIS:IsKeyDown(Enum.KeyCode.D)
end)

Again, any ideas? Thanks!

1 Like

task.spawn (not regular spawn) the DashEffect when using it, local dashTask = task.spawn(dashEffect)

and cancel the dashTask whenever you jump task.cancel(dashTask)