How can I make it so you can jump midair while doing a roll

I’m trying to make a roll mechanic for my game.
The roll script does work, however I wanna make it so that when you’re rolling, you can jump out of the roll animation, while keeping the velocity from rolling.

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character
local humanoid = char:FindFirstChild('Humanoid')
if humanoid then
	local slideAnim = Instance.new("Animation")
	slideAnim.AnimationId = "rbxassetid://12868749863"

	local keybind = Enum.KeyCode.LeftShift
	local canslide = true

	UIS.InputBegan:Connect(function(input,gameprocessed)
		if gameprocessed then return end
		if not canslide then return end
		if humanoid.FloorMaterial == Enum.Material.Air then return end
		if input.KeyCode == keybind then
			canslide = false

			local playAnim = humanoid:LoadAnimation(slideAnim)
			playAnim:Play()
			local sound = Instance.new('Sound')
			sound.Name = "bob"
			sound.Parent = char.HumanoidRootPart
			sound.SoundId = "rbxassetid://7545317681"
			sound:Play()

			local slide = Instance.new("BodyVelocity")
			slide.MaxForce = Vector3.new(0.5,0,0.5) * 30000
			slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 70
			slide.Parent = char.HumanoidRootPart

			for count = 1, 15 do
				wait(0.01)
				slide.Velocity*= 0.9
			end
			playAnim:Stop()
			slide:Destroy()
			canslide = true
		end
	end)

end

(the script is in startercharacterscripts)

Interesting concept!

I suggest, inside the for loop, use a function to see if the player has jumped during this time where the velocity is being handled and break the loop immediately after the jump has concluded.

so in the for loop, should i use something like

UIS.JumpRequest:Connect(function(
break
*funny code*
end)

or something similar?

You make a state for rolling, and when the jump function gets called, check the state.

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild('Humanoid')
local jumps = 0
local max = 2
local isjump = false
local currenttick = tick()
local db = false
if humanoid then
	local slideAnim = Instance.new("Animation")
	slideAnim.AnimationId = "rbxassetid://12868749863"
	local jumpAnim = Instance.new('Animation')
	jumpAnim.AnimationId = "rbxassetid://12886271444"
	local function onstateChanged(oldstate,newstate)
		
		
		if Enum.HumanoidStateType.Landed == newstate then
			print('LANDED')
			wait(0.2)
			jumps = 0
			db = false
		end
		
		if Enum.HumanoidStateType.Freefall == newstate then
			wait(0.2)
			db = true
				print('fall')
		end
		
			
		if Enum.HumanoidStateType.Jumping == newstate then
			db = false
			jumps +=1
				print('jumping')
		end
	end
	local playAnim = humanoid:LoadAnimation(slideAnim)
	local function onJump()
		local jumpanimation = humanoid:LoadAnimation(jumpAnim)
		if humanoid.FloorMaterial == Enum.Material.Air then return end
		if db == false then
			slideAnim:Destroy()
			jumpanimation:Play()
			if jumps < max and tick() > 0.2 then
				currenttick = tick()
				humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
		end
	end
	

	local keybind = Enum.KeyCode.LeftShift
	local canslide = true

	UIS.InputBegan:Connect(function(input,gameprocessed)
		if gameprocessed then return end
		if not canslide then return end
		if humanoid.FloorMaterial == Enum.Material.Air then
			return
		end
		if input.KeyCode == keybind then
			canslide = false

			local playAnim = humanoid:LoadAnimation(slideAnim)
			playAnim:Play()
			local sound = Instance.new('Sound')
			sound.Name = "bob"
			sound.Parent = char.HumanoidRootPart
			sound.SoundId = "rbxassetid://7545317681"
			sound:Play()

			local slide = Instance.new("BodyVelocity")
			slide.MaxForce = Vector3.new(0.5,0,0.5) * 30000
			slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 70
			slide.Parent = char.HumanoidRootPart
			UIS.JumpRequest:Connect(onJump)
			humanoid.StateChanged:Connect(onstateChanged)

			for count = 1, 15 do
				wait(0.01)
				slide.Velocity*= 0.9
			end
			playAnim:Stop()
			slide:Destroy()
			canslide = true
		end
	end)

end

i made it so you can jump out of the roll now, but im still stumped on making it jump while midair and keeping the velocity.

	local function onJump()
		local jumpanimation = humanoid:LoadAnimation(jumpAnim)
		if db == false and isrolling == true then
			slideAnim:Destroy()
			jumpanimation:Play()
			local slide2 = Instance.new("BodyVelocity")
			slide2.MaxForce = Vector3.new(0.5,0,0.5) * 30000
			slide2.Velocity = char.HumanoidRootPart.CFrame.lookVector * 50
			slide2.Parent = char.HumanoidRootPart
			
			
			for count = 1,10 do
				wait(0.05)
				slide2.Velocity *= 0.9
			end
			slide2:Destroy()
			if jumps < max and tick() > 0.2 then
				currenttick = tick()
				
			end
		end
	end
	

a bit of progress, but now the velocity can almost be kept when you roll-jump, but the velocity doesn’t really apply if you dont jump at the end of the roll.