Help with Double Jump Script

I am trying to make a Double Jump Mechanism (Self explanatory, 2 consecutive clicks on the spacebar allows the player to jump midair).

My issue is, I made a custom Jump Animation and it looks like this when I finished it -
https://gyazo.com/5363f7e1cef3460979800213277f3560

But when I try and double jump, the animation gets cut off halfway like this-
https://gyazo.com/a5cbd6075d3d2a7139f9a02c78f27860

My code:-

local UserInputService = game:GetService("UserInputService")
local localPlayer = game.Players.LocalPlayer
local character 
local humanoid

local canDoubleJump = false
local hasDoubleJumped = false
local oldPower
local TIME_BETWEEN_JUMPS = 0.2
local DOUBLE_JUMP_POWER_MULTIPLIER = 2
local animateScript

function getAnimate(character, humanoid)
	for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
		playingTracks:Stop(0)
	end
	
	animateScript = character:WaitForChild("Animate")
end

function onJumpRequest()
	if not character or not humanoid or not character:IsDescendantOf(workspace) or
		humanoid:GetState() == Enum.HumanoidStateType.Dead then
		return
	end

	if canDoubleJump and not hasDoubleJumped then
		hasDoubleJumped = true
		humanoid.JumpPower = oldPower * DOUBLE_JUMP_POWER_MULTIPLIER
		animateScript.jump.JumpAnim.AnimationId = "rbxassetid://7196819435"	
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
end

local function characterAdded(newCharacter)
	character = newCharacter
	humanoid = newCharacter:WaitForChild("Humanoid")
	hasDoubleJumped = false
	canDoubleJump = false
	oldPower = humanoid.JumpPower
	
	getAnimate(character, humanoid)

	humanoid.StateChanged:connect(function(old, new)
		if new == Enum.HumanoidStateType.Landed then
			canDoubleJump = false
			hasDoubleJumped = false
			humanoid.JumpPower = oldPower
			animateScript.jump.JumpAnim.AnimationId = "rbxassetid://125750702"
		elseif new == Enum.HumanoidStateType.Freefall then
			wait(TIME_BETWEEN_JUMPS)
			canDoubleJump = true
		end
	end)
end

if localPlayer.Character then
	characterAdded(localPlayer.Character)
end

localPlayer.CharacterAdded:connect(characterAdded)
UserInputService.JumpRequest:connect(onJumpRequest)

Any help will be appreciated!
(I am also a beginner so there might be a easy solution I don’t know of)

Have you replayed the animation after you doublejump?

1 Like

I haven’t tried that but I’ll see.

Oop- It worked. Thanks a lot :slight_smile: