Dive Script [HELP]

I’ve tried and failed miserably so I was wondering if anyone can / knows how to do this.

I have a script and whenever you press R the Player Rolls based on the key their holding. However, it works while the player is in the air. I was wondering if anyone knows how to make it so the rolling doesn’t activate on the ground, and; if it can work the same functionally but play a single, separate animation, that only goes forward. Which will be a dive:

StarterCharacterScripts:

LocalScript:

-- Get Necessary Services --
local RS = game:GetService("ReplicatedStorage")
local VfxFolder = RS:WaitForChild("VfxFolder")
local DashClone = VfxFolder:WaitForChild("DashClone")
local UIS = game:GetService("UserInputService")
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 DashTime = 0.5

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

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

-- Handle Input for Dashing --
UIS.InputBegan:Connect(function(Key, IsTyping)
	if IsTyping then return end
	if Key.KeyCode == Enum.KeyCode.R then
		if DashDeb == false and Char:FindFirstChild("Deb") == nil then
			DashDeb = true
			delay(DashTime + 2.5, function()
				DashDeb = false
			end)

			-- Dash Forward --
			if WKeyDown then
				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
				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
				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
				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)

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

Child Script:

--- Getting the RemoteEvent --
local Event = script.Parent:WaitForChild("Event")

-- Handling RemoteEvent from the client --
Event.OnServerEvent:Connect(function(Player, Events)

	-- Get character and humanoid root part --
	local Char = Player.Character
	local HRP = Char.HumanoidRootPart

	-- Get the dash sound --
	local Sound = script.Dash

	-- Dash Event --
	if Events == "Dash" then
		-- Play the dash sound --
		Sound:Play()
		wait(0.8)
		-- Stop the dash sound --
		Sound:Stop()
	end
end)

Thank you!

I’ve made some progress on the script but for some reason the player rolls after the player dives. Anyone know why?

-- 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 DashTime = 0.5
local DivingDeb = false
local DivingTime = 0.5
-- 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://0"

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

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

	-- Activate Dive with R key if in the air
	if Key.KeyCode == Enum.KeyCode.R then
		if not DivingDeb and isInAir() then
			DivingDeb = true
			local playAnim = Char.Humanoid:LoadAnimation(DiveAnim)
			playAnim:Play()

			-- Create BodyVelocity for diving --
			local dive = Instance.new("BodyVelocity")
			dive.MaxForce = Vector3.new(50000, 50000, 50000)
			dive.Velocity = HRP.CFrame.LookVector * 200
			dive.Parent = HRP

			-- Gradual dive deceleration --
			for count = 1, 5 do
				wait(0.1)
				dive.Velocity = dive.Velocity * 0.8
			end
			playAnim:Stop()
			dive:Destroy()
			wait(1.5)
			DivingDeb = false
		end
	end

	-- Handle Dash Inputs (already present in previous script) --
	if Key.KeyCode == Enum.KeyCode.R then
		if DashDeb == false and Char:FindFirstChild("Deb") == nil then
			DashDeb = true
			delay(DashTime + 2.5, function()
				DashDeb = false
			end)

			-- Dash Forward --
			if WKeyDown then
				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
				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
				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
				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)

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

It looks like you are running the dive event, and then immediately after the script is running the second event. Forming this into an “if else” would hopefully resolve that issue.

if Key.KeyCode == Enum.KeyCode.R then
    if not DivingDeb and isInAir() then
    -- dive logic
    elseif DashDeb == false and Char:FindFirstChild("Deb") == nil then
    -- dash logic
end
1 Like

Thanks for picking up on. Here’s the finished 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 DashTime = 0.5
local DivingDeb = false
local DivingTime = 0.5
-- 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://0"

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

-- 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
	if Key.KeyCode == Enum.KeyCode.R then
		if not DivingDeb and isInAir() then
			DivingDeb = true
			local playAnim = Char.Humanoid:LoadAnimation(DiveAnim)
			playAnim:Play()

			-- Create BodyVelocity for diving --
			local dive = Instance.new("BodyVelocity")
			dive.MaxForce = Vector3.new(50000, 50000, 50000)
			dive.Velocity = HRP.CFrame.LookVector * 200
			dive.Parent = HRP

			-- Gradual dive deceleration --
			for count = 1, 5 do
				wait(0.1)
				dive.Velocity = dive.Velocity * 0.8
			end
			playAnim:Stop()
			dive:Destroy()
			wait(1.5)
			DivingDeb = false
		elseif DashDeb == false and Char:FindFirstChild("Deb") == nil then
			-- Dash Logic --
			DashDeb = true
			delay(DashTime + 2.5, function()
				DashDeb = false
			end)

			-- Dash Forward --
			if WKeyDown then
				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
				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
				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
				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)

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