I have a dash and diving script however it doesn’t apply the velocity when pressing space while diving, but does when dashing, even though it should work on both. Any ideas?
The script is a local script in StarterCharacterScript.
Area with issue:
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
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)
-- Get current height relative to the starting position
local currentHeight = HRP.Position.Y - baseY
-- Apply Y-axis boost ONCE when space is pressed
if UIS:IsKeyDown(Enum.KeyCode.Space) and (DashDeb or DivingDeb) and currentHeight < heightLimit and not hasBoosted then
hasBoosted = true -- Mark that we've already boosted
-- Apply a temporary upward force using BodyVelocity
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = Vector3.new(0, 20, 0) -- Jump strength
bodyVelocity.MaxForce = Vector3.new(0, math.huge, 0) -- Only affects Y-axis
bodyVelocity.Parent = HRP
activeBoost = bodyVelocity -- Store it so we can cancel if needed
-- Destroy BodyVelocity after 0.5 seconds
task.delay(0.25, function()
if activeBoost == bodyVelocity then -- Ensure it's the current boost
bodyVelocity:Destroy()
activeBoost = nil
end
end)
-- Stop dash animations
ForwardDashAnime:Stop()
LeftDashAnime:Stop()
BackDashAnime:Stop()
RightDashAnime:Stop()
end
-- Reset boost when the player lands
if Hum:GetState() == Enum.HumanoidStateType.Landed then
hasBoosted = false -- Allow boosting again when landing
baseY = HRP.Position.Y -- Update base height
end
-- Cancel boost if DashDeb or DivingDeb turns false
if not DashDeb and not DivingDeb then
if activeBoost then
activeBoost:Destroy()
activeBoost = nil
end
hasBoosted = false -- Reset boost availability
end
end)
Full 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
-- 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)
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
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)
-- Get current height relative to the starting position
local currentHeight = HRP.Position.Y - baseY
-- Apply Y-axis boost ONCE when space is pressed
if UIS:IsKeyDown(Enum.KeyCode.Space) and (DashDeb or DivingDeb) and currentHeight < heightLimit and not hasBoosted then
hasBoosted = true -- Mark that we've already boosted
-- Apply a temporary upward force using BodyVelocity
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = Vector3.new(0, 20, 0) -- Jump strength
bodyVelocity.MaxForce = Vector3.new(0, math.huge, 0) -- Only affects Y-axis
bodyVelocity.Parent = HRP
activeBoost = bodyVelocity -- Store it so we can cancel if needed
-- Destroy BodyVelocity after 0.5 seconds
task.delay(0.25, function()
if activeBoost == bodyVelocity then -- Ensure it's the current boost
bodyVelocity:Destroy()
activeBoost = nil
end
end)
-- Stop dash animations
ForwardDashAnime:Stop()
LeftDashAnime:Stop()
BackDashAnime:Stop()
RightDashAnime:Stop()
end
-- Reset boost when the player lands
if Hum:GetState() == Enum.HumanoidStateType.Landed then
hasBoosted = false -- Allow boosting again when landing
baseY = HRP.Position.Y -- Update base height
end
-- Cancel boost if DashDeb or DivingDeb turns false
if not DashDeb and not DivingDeb then
if activeBoost then
activeBoost:Destroy()
activeBoost = nil
end
hasBoosted = false -- Reset boost availability
end
end)
Thanks!