Hello there,
I have been working on this double jump system for my simulator project. The double jump system works perfectly but, I implemented an animation to along with it (A Backflip). For some reason, when the player jumps, the animation plays but halfway/near the end of the animation, it stops playing. Sometimes, it may fully play the animation but it does not do it consistently. Any assistance is helpful. I will attach the code.
What do I think may be causing it?
-
I think that when the player is in the falling state, it cancels the animation. If that was true, I would have to find away to make the Jumping state longer so the animation can fully play then the player can fall. (That would require me to increase the double jump multiplier.
-
It may be because I need to implement a wait between the jumping state and animation play. I tried doing this and the issue was not necessarily fixed but the animation was able to play longer.
The parent of the code is a local script inside of starter character scripts
Code
--//Locals
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.1
local DOUBLE_JUMP_POWER_MULTIPLIER = 2.1
--//Functions/MainScript
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
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://6346485498"
local animation = humanoid:LoadAnimation(Anim)
animation:Play()
end
end
local function characterAdded(newCharacter)
character = newCharacter
humanoid = newCharacter:WaitForChild("Humanoid")
hasDoubleJumped = false
canDoubleJump = false
oldPower = humanoid.JumpPower
humanoid.StateChanged:connect(function(old, new)
if new == Enum.HumanoidStateType.Landed then
canDoubleJump = false
hasDoubleJumped = false
humanoid.JumpPower = oldPower
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)
Video
robloxapp-20210210-1640041.wmv (2.8 MB)