It looks like your current implementation has some issues with handling the jump delay and the landing animation due to the nature of state transitions in humanoids. Specifically, if the character hits something above them, the states might change too frequently, causing the landing animation to replay and the jump delay to behave irregularly. Here’s how you can address these problems:
1. Jump Delay
For the jump delay not counting when hitting something above, you should consider implementing a condition to check if the player is grounded before disabling the jump. This can be done by checking the Humanoid.FloorMaterial property.
2. Landing Animation
For the landing animation replaying over and over, you might want to introduce a debounce mechanism to ensure that the landing animation doesn't replay multiple times in a short period.
Here's an improved version of your script incorporating these changes:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Humanoid = script.Parent:WaitForChild("Humanoid") -- Ensure this script is placed properly.
local canJump = true
local debounce = false
local sprinting = false
local playAnim = Humanoid:LoadAnimation(ReplicatedStorage:WaitForChild("Animations"):WaitForChild("Player"):WaitForChild("Land"))
local animTrack = ReplicatedStorage:WaitForChild("Animations"):WaitForChild("Player"):WaitForChild("Run") -- Assuming there's a run animation.
local Settings = {
JumpCooldown = 2 -- Example value, replace with your actual setting.
}
Humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Jumping and canJump then
canJump = false
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
task.wait(Settings.JumpCooldown)
canJump = true
Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
elseif newState == Enum.HumanoidStateType.Landed and not debounce then
debounce = true
if oldState == Enum.HumanoidStateType.Freefall then -- Ensure it transitioned from Freefall
playAnim:Play()
playAnim.Stopped:Wait()
if sprinting then
animTrack:Play()
end
task.wait(0.5) -- Cooldown to prevent multiple plays in quick succession.
end
debounce = false
end
end)
Explanation:
-
Jump Cooldown with Ground Check:
- The script sets
canJump to false and disables the jump state for a specified cooldown time.
- After the cooldown time, it re-enables the jump state and sets
canJump back to true.
-
Debounce for Landing Animation:
- A debounce mechanism (
debounce variable) is introduced to prevent the landing animation from playing multiple times in quick succession.
- The animation only plays if the old state was
Freefall, ensuring that the landing animation doesn't play when the player just steps off a ledge or hits something above them.
- An additional wait time is added between animations to provide a buffer.
-
Sprint and Animation Transition:
- It checks if
sprinting is true and plays the appropriate animation.
With these adjustments, the issues with jumping delay and redundant landing animations should be resolved. Ensure to replace placeholders such as Settings.JumpCooldown with your actual settings and to adjust animation references appropriately.