Roll and Dive Script

I made a Direction Roll script and it works fine however I wanted to add diving to the script. I attempted to do this by making the Roll animation only work on the ground and then play a different animation when the player is in the air however this failed.

I spent a few hours working on it and it slowly just went to a mess of spaghetti code that ended up with the entire thing not working. So, I was wondering if anyone can add the addition I said above to the script.

Here is the original script local script is StarterCharacterScripts:

-- 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 the local player and their character
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()

-- Get references to the humanoid and the root part of the character
local Hum = Char:WaitForChild("Humanoid")
local HRP = Char:WaitForChild("HumanoidRootPart")

-- Variables to track key states
local WKeyDown = false
local AKeyDown = false
local SKeyDown = false
local DKeyDown = false

-- Variables to 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 to signal 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
			-- Set dash debounce
			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
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)

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

And a Script who’s parent script is the script above:

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

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

Picture incase you’re confused:
image

If you have any questions just ask, I’m probably heading off for the night so sorry if I don’t respond in a timely manor.

Thanks!

1 Like

Sorry for the late response!

So there’s ALOT you can do to fix this spaghetti code, one of them is using the Humanoid MoveDirection instead of detecting which keybind is being held.

Also not entirely sure what you need help with for diving, it seems you already have a system for it? If you need help with the script don’t hesitate to ask by the way!

Over the past day I’ve fixed a lot of issues and got it all worked out, forgot this post was still up. I’ll take your tip and use the Humanoid MoveDirection. Thank you!

1 Like