I have a dash and dive script and whenever the player presses space during on of these actions it should apply a velocity boost, which it does for dashing but not for diving. I was just curious why it doesn’t for diving, any ideas on how to fix this? Thanks!
Script (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:GetService("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")
-- Global variables for boost logic --
local heightLimit = 8 -- Maximum height in studs
local baseY = HRP.Position.Y -- Store the starting Y position
local hasBoosted = false -- Prevents multiple boosts in one jump
local activeBoost = nil -- Stores the active BodyVelocity
-- New variable to track active dash state --
local isDashing = false
-- 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, Diving, and Boosting --
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
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()
local dive = Instance.new("BodyVelocity")
dive.MaxForce = Vector3.new(50000, 50000, 50000)
local diveVelocity = HRP.CFrame.LookVector * 45 + Vector3.new(0, 20, 0)
dive.Velocity = diveVelocity
dive.Parent = HRP
local hitWall = false
RunService.Heartbeat:Connect(function()
if dive and not hitWall then
local forwardDirection = HRP.CFrame.LookVector
local rayDirection = forwardDirection * 3
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {Char}
rayParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(HRP.Position + Vector3.new(0, 2, 0), rayDirection, rayParams)
if raycastResult and raycastResult.Instance and raycastResult.Instance.CanCollide then
dive:Destroy()
playAnim:Stop()
hitWall = true
end
end
end)
while isInAir() do
if isSwimming() then
playAnim:Stop()
dive:Destroy()
break
end
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)
wait(0.1)
end
if not isInAir() then
playAnim:Stop()
ForwardDashAnime:Play()
end
playAnim:Stop()
dive:Destroy()
CanDashAfterDive = true
task.delay(0.5, function()
CanDashAfterDive = false
end)
task.delay(DiveCooldownTime, function()
DivingDeb = false
end)
RollDiveCooldown = true
task.delay(RollDiveCooldownTime, function()
RollDiveCooldown = false
end)
elseif DashDeb == false and not isInAir() and Char:FindFirstChild("Deb") == nil and not CanDashAfterDive then
DashDeb = true
-- Dash Forward
if WKeyDown then
if isInAir() then return end
isDashing = true
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")
task.delay(DashTime, function()
isDashing = false
end)
delay(DashTime + 0.1, function()
ForwardDashAnime:Stop()
end)
-- Dash Left
elseif AKeyDown then
if isInAir() then return end
isDashing = true
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")
task.delay(DashTime, function()
isDashing = false
end)
delay(DashTime + 0.1, function()
LeftDashAnime:Stop()
end)
-- Dash Backward
elseif SKeyDown then
if isInAir() then return end
isDashing = true
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")
task.delay(DashTime, function()
isDashing = false
end)
delay(DashTime + 0.1, function()
BackDashAnime:Stop()
end)
-- Dash Right
elseif DKeyDown then
if isInAir() then return end
isDashing = true
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")
task.delay(DashTime, function()
isDashing = false
end)
delay(DashTime + 0.1, function()
RightDashAnime:Stop()
end)
end
task.delay(DashCooldownTime, function()
DashDeb = false
end)
end
end
-- Handle Boost on Spacebar Press --
if Key.KeyCode == Enum.KeyCode.Space then
local currentHeight = HRP.Position.Y - baseY
-- Boost applies if either a dash or dive is active
if (isDashing or DivingDeb) and currentHeight < heightLimit and not hasBoosted then
hasBoosted = true
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = Vector3.new(0, 20, 0)
bodyVelocity.MaxForce = Vector3.new(0, math.huge, 0)
bodyVelocity.Parent = HRP
activeBoost = bodyVelocity
task.delay(0.25, function()
if activeBoost == bodyVelocity then
bodyVelocity:Destroy()
activeBoost = nil
end
end)
ForwardDashAnime:Stop()
LeftDashAnime:Stop()
BackDashAnime:Stop()
RightDashAnime:Stop()
end
end
end)
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)
if Hum:GetState() == Enum.HumanoidStateType.Landed then
hasBoosted = false
baseY = HRP.Position.Y
end
-- Modified cleanup: only remove boost when not dashing or diving
if not (isDashing or DivingDeb) then
if activeBoost then
activeBoost:Destroy()
activeBoost = nil
end
hasBoosted = false
end
end)
Sorry for the spaghetti code, I just wanted to finish the script and bugs before shortening it down.
Thanks again!