I have a double jump script and was just wondering if anyone could help improve it to fix an issue that I am experiencing. All I need is for someone to add something to the script that cuts the animation short if the player is already on the ground. I want this done because if you double jump to a high place and land the rest of the animation will continue playing even after you land. Thank you for your help, just ask if you need more information.
Here is the script:
local UIS = game:GetService("UserInputService")
local CanDoubleJump = true
local CanSetCanDoubleJumpToTrue = false
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
UIS.JumpRequest:Connect(function()
if CanDoubleJump then
Character:WaitForChild("Humanoid").JumpPower = Character:WaitForChild("Humanoid").JumpPower * 1
Character:WaitForChild("Humanoid"):ChangeState(Enum.HumanoidStateType.Jumping)
local Animator = Character:WaitForChild("Humanoid"):FindFirstChild("Animator") or Instance.new("Animator",Character:WaitForChild("Humanoid"))
Animator:LoadAnimation(script.Flip):Play()
CanDoubleJump = false
end
end)
game.Players.LocalPlayer.Character:WaitForChild("Humanoid").StateChanged:Connect(function(old,new)
if new == Enum.HumanoidStateType.Landed then
CanDoubleJump = false
CanSetCanDoubleJumpToTrue = true
Character:WaitForChild("Humanoid").JumpPower = game:GetService("StarterPlayer").CharacterJumpPower
elseif new == Enum.HumanoidStateType.Freefall then
if CanSetCanDoubleJumpToTrue == true then
wait(0.2)
CanDoubleJump = true
CanSetCanDoubleJumpToTrue = false
end
end
end)
if humanoid:GetState == Enum.HumanoidStateType.Landed or humanoid:GetState == Enum.HumanoidStateType.Running then
if doubleJump.IsPlaying then
doubleJump:Stop()
end
end
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local StarterPlayer = game:GetService("StarterPlayer")
local player = Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
type Animator = Animator & {
LoadAnimation: (self: Animator, animation: Animation) -> AnimationTrack,
}
type AnimationTrack = AnimationTrack & {
Play: (self: AnimationTrack) -> nil,
Stop: (self: AnimationTrack) -> nil,
IsPlaying: boolean,
}
local CanDoubleJump: boolean = true
local CanSetCanDoubleJumpToTrue: boolean = false
local DoubleJumpAnimation: AnimationTrack? = nil
local Animator: Animator? = nil
local function loadDoubleJumpAnimation(): AnimationTrack
if not DoubleJumpAnimation then
Animator = Humanoid:FindFirstChild("Animator") or Instance.new("Animator", Humanoid)
DoubleJumpAnimation = Animator:LoadAnimation(script.Flip)
end
return DoubleJumpAnimation
end
local function handleJumpRequest()
if CanDoubleJump then
Humanoid.JumpPower = Humanoid.JumpPower * 1
Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
local animation = loadDoubleJumpAnimation()
animation:Play()
CanDoubleJump = false
end
end
local function handleStateChange(oldState: Enum.HumanoidStateType, newState: Enum.HumanoidStateType)
if newState == Enum.HumanoidStateType.Landed then
CanDoubleJump = false
CanSetCanDoubleJumpToTrue = true
Humanoid.JumpPower = StarterPlayer.CharacterJumpPower
if DoubleJumpAnimation and DoubleJumpAnimation.IsPlaying then
DoubleJumpAnimation:Stop()
end
elseif newState == Enum.HumanoidStateType.Freefall then
if CanSetCanDoubleJumpToTrue then
task.wait(0.2)
CanDoubleJump = true
CanSetCanDoubleJumpToTrue = false
end
end
end
local function onJumpRequestError(err)
warn("Error in handleJumpRequest: " .. tostring(err))
end
local function onStateChangeError(err)
warn("Error in handleStateChange: " .. tostring(err))
end
UIS.JumpRequest:Connect(function()
local success, err = pcall(handleJumpRequest)
if not success then
onJumpRequestError(err)
end
end)
Humanoid.StateChanged:Connect(function(oldState, newState)
local success, err = pcall(handleStateChange, oldState, newState)
if not success then
onStateChangeError(err)
end
end)