Roll Jump kind of queues itself until I jump

I made a roll-jump script that allows you to roll, and jump out of the roll, even midair, while keeping the velocity from the roll.

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 = 1
local max = 2
local isjump = false
local currenttick = tick()
local db = false
local canjump = true
local isrolling = false
if humanoid then
	local slideAnim = Instance.new("Animation")
	slideAnim.AnimationId = "rbxassetid://12868749863"
	local jumpAnim = Instance.new('Animation')
	jumpAnim.AnimationId = "rbxassetid://12900868359"
	local landAnim = Instance.new('Animation')
	landAnim.AnimationId = "rbxassetid://12901080333"
	local function onstateChanged(oldstate,newstate)
		
		
		if Enum.HumanoidStateType.Landed == newstate then
			wait(0.2)
			jumps = 1
			db = false
		end
		
		if Enum.HumanoidStateType.Freefall == newstate then
			wait(0.2)
			db = true
			isrolling = false
		end
		
			
		if Enum.HumanoidStateType.Jumping == newstate then
			db = false
			jumps +=1
		end
	end
	local playAnim = humanoid:LoadAnimation(slideAnim)
	local function onJump()
		local jumpanimation = humanoid:LoadAnimation(jumpAnim)
		local landanimation = humanoid:LoadAnimation(landAnim)
		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 * 40
			slide2.Parent = char.HumanoidRootPart
			if humanoid.FloorMaterial == Enum.Material.Air then
				
				print("Huh")
				if jumps < max and tick() > 0.5 then
					humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
					print("WHAT")
					currenttick = tick()
					if Enum.HumanoidStateType.Landed then
						jumpAnim:Destroy()
						slide2:Destroy()
					end
					wait(0.3)
					jumpAnim:Destroy()
					slide2:Destroy()
				end
				
			end
			print('Jumping')
			wait(0.5)
			if Enum.HumanoidStateType.Landed then
				jumpAnim:Destroy()
				slide2:Destroy()
			end
			print('END')
			wait(0.3)
			jumpAnim:Destroy()
			slide2:Destroy()
		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
			isrolling = true

			local playAnim = humanoid:LoadAnimation(slideAnim)
			playAnim:Play()
			UIS.JumpRequest:Connect(onJump)
			humanoid.StateChanged:Connect(onstateChanged)
			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 * 60
			slide.Parent = char.HumanoidRootPart
			wait(0.4)
			local canjump = false
			playAnim:Stop()
			slide:Destroy()
			wait(0.5)
			canslide = true
		end
	end)

end

It almost works, but for some reason, when I roll, it kind of queues the “roll-jump” for a while, even after the roll anim has finished.
someone help pls

I’ve refactored your code and changed some surface-level errors, however, I don’t have animations to fully see whether I fixed the issue. Try out this code to see if it fixes the problem:

--// Services //--
local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")

--// Player & Character //--
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")
local humanoid = char:WaitForChild('Humanoid')
local animator = humanoid.Animator

--// Settings //--
local keybind = Enum.KeyCode.LeftShift
local jumps, max = 1, 2

--// Values //--
local canSlide, isSliding = true, false
local currentTick = tick()
local db = false

--// Instances //--
local slideForce

--// Animations //--
--// Load animations only once:
local slideAnim = Instance.new("Animation")
slideAnim.AnimationId = "rbxassetid://12868749863"
slideAnim = animator:LoadAnimation(slideAnim) --// Humanoid:LoadAnimation() is deprecated; use Animator:LoadAnimation()

local jumpAnim = Instance.new('Animation')
jumpAnim.AnimationId = "rbxassetid://12900868359"
jumpAnim = animator:LoadAnimation(jumpAnim)

local landAnim = Instance.new('Animation')
landAnim.AnimationId = "rbxassetid://12901080333"
landAnim = animator:LoadAnimation(landAnim)

--// On Humanoid state type changed:
local function OnStateChanged(_, newState)
	if Enum.HumanoidStateType.Landed == newState then
		task.wait(0.2) --// task.wait() is better than wait()
		jumps = 1
		db = false
	end

	if Enum.HumanoidStateType.Freefall == newState then
		task.wait(0.2)
		isSliding = false
		db = true
	end

	if Enum.HumanoidStateType.Jumping == newState then
		jumps += 1
		db = false
	end
end

--// On player jump request:
local function OnJump()
	if not db and isSliding then
		slideAnim:Stop()
		jumpAnim:Play()
		
		--// Give meaningful names to values:
		local jumpSlideForce = Instance.new("BodyVelocity")
		jumpSlideForce.MaxForce = Vector3.new(0.5, 0, 0.5) * 30000
		jumpSlideForce.Velocity = hrp.CFrame.lookVector * 40
		jumpSlideForce.Parent = hrp

		if humanoid.FloorMaterial == Enum.Material.Air then

			print("Huh")
			if jumps < max and tick() > 0.5 then
				humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
				print("WHAT")
				
				currentTick = tick()
				if Enum.HumanoidStateType.Landed then
					jumpAnim:Stop()
					jumpSlideForce:Destroy()
				end
				
				task.wait(0.3)
				
				jumpAnim:Stop()
				jumpSlideForce:Destroy()
			end

		end
		
		print('Jumping')
		task.wait(0.5)
		if Enum.HumanoidStateType.Landed then
			jumpAnim:Stop()
			jumpSlideForce:Destroy()
		end
		
		print('END')
		task.wait(0.3)
		
		jumpAnim:Stop()
		jumpSlideForce:Destroy()
	end
end

--// Start sliding
local function Slide()
	canSlide = false
	isSliding = true

	slideAnim:Play()

	local sound = Instance.new('Sound')
	sound.Name = "bob"
	sound.Parent = hrp
	sound.SoundId = "rbxassetid://7545317681"
	sound:Play()

	--// Destroy the sound when it is done playing; so you don't fill the character with sound instances:
	sound.Ended:Connect(function()
		sound:Destroy()
	end)
	
	--// Create the force that pushes character forward:
	slideForce = Instance.new("BodyVelocity")
	slideForce.MaxForce = Vector3.new(0.5, 0, 0.5) * 30000
	slideForce.Velocity = hrp.CFrame.LookVector * 60
	slideForce.Parent = hrp
end

--// Stop slide and cooldown
local function EndSlide()
	slideAnim:Stop()
	slideForce:Destroy()

	task.wait(0.5)

	canSlide = true
end

local function OnSlide(_, state, input)
	if state == Enum.UserInputState.Begin then
		
		--// Condense conditional statements to a single line:
		if (canSlide) and (input.KeyCode == keybind) and (humanoid.FloorMaterial ~= Enum.Material.Air) then
			Slide()
			task.wait(0.4)
			EndSlide()
		end
		
	end
end

--// Only connect these events once:
UIS.JumpRequest:Connect(OnJump)
humanoid.StateChanged:Connect(OnStateChanged)

--// Context Action Service is preferable in this case:
CAS:BindAction("Slide", OnSlide, true, keybind)

Hope this helps! :slight_smile:

If it doesn’t work just reply and I’ll take another look at it.

Your roll-jump system is pretty solid, just needed some modifications. Mainly changing wait() to task.wait() and only connecting functions and animations once. Here are some resources that might help:

Thank you, but I dont think the new code kinda solved the queueing jump problem
robloxapp-20230329-0930428.wmv (4.1 MB)
It still works properly, but it still sometimes applies the velocity when I jump after the roll.

nvm im an idiot I just had to set isSliding to false in one of the fucntions

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.