Cancel Script If Touching Wall? [HELP]

I’ve tried to implement this but failed terribly so I was wondering if anyone could help. Basically its a dashing and diving script and I wanted to make it so that if the player hits a wall while diving, not dashing, it completely cancels the dive. Again, I’ve tried to do this but failed.

Ignore the spaghetti code.

Script:

-- 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

	-- 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

			-- 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 and not isInAir() then  -- Prevent 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 and not isInAir() then  -- Prevent 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 and not isInAir() then  -- Prevent 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 and not isInAir() then  -- Prevent 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

	-- Space cancels rolling only if rolling (DashDeb is true)
	if Key.KeyCode == Enum.KeyCode.Space and DashDeb == true then
		-- Stop any currently playing dash animations
		ForwardDashAnime:Stop()
		BackDashAnime:Stop()
		LeftDashAnime:Stop()
		RightDashAnime:Stop()

		endDash() -- Cancel dash if Space is pressed during a dash
	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)

It’s a LocalScript in StarterCharacterScripts. Thanks!

Im guessing this is a dash script. There’s a few solutions I can think of.

Touched events and raycasting.

Usually you can’t rely on touched events since they don’t register some times and are very bad to use in this case.

So I would go for raycasting. I guess shoot a ray from the HumanoidRootPart to the direction of the dash and check the distance, if the rays length is 10 studs or less. Stop the dash.

2 Likes

I did this and it just cancels immediately, even when not touching a wall. Any idea why?

Script:

-- 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

	-- 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

			-- Monitor collision and stop the dive if the player hits a wall
			local hitWall = false
			RunService.Heartbeat:Connect(function()
				if dive and not hitWall then
					-- Only cancel the dive if hitting a solid object, not air
					local raycastResult = workspace:Raycast(HRP.Position, dive.Velocity.unit * 2, RaycastParams.new())
					if raycastResult and raycastResult.Instance and raycastResult.Instance.CanCollide then
						-- Stop the dive if a wall or solid object is hit
						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 and not isInAir() then  -- Prevent 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 and not isInAir() then  -- Prevent 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 and not isInAir() then  -- Prevent 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 and not isInAir() then  -- Prevent 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)

	-- Space cancels rolling only if rolling (DashDeb is true)
	if Key.KeyCode == Enum.KeyCode.Space and DashDeb == true then
		-- Stop any currently playing dash animations
		ForwardDashAnime:Stop()
		BackDashAnime:Stop()
		LeftDashAnime:Stop()
		RightDashAnime:Stop()

		-- Cancel dash and start cooldown
		endDash() 
	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)

Don’t worry, I fixed it now, thanks for the recommendation. Script if you’re interested:

-- 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

	-- 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 and not isInAir() then  -- Prevent 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 and not isInAir() then  -- Prevent 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 and not isInAir() then  -- Prevent 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 and not isInAir() then  -- Prevent 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

	-- Space cancels rolling only if rolling (DashDeb is true)
	if Key.KeyCode == Enum.KeyCode.Space and DashDeb == true then
		-- Stop any currently playing dash animations
		ForwardDashAnime:Stop()
		LeftDashAnime:Stop()
		BackDashAnime:Stop()
		RightDashAnime:Stop()
		DashDeb = false
		print("Dash stopped")
	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)
1 Like