Play animation when character falls too long [Fail]

I have written this code, but sometimes the animation does not play when it should. I really have no idea what is wrong.

When the character falls for more than one second, an animation should play until it stops falling.

local State = {
	ToPlay = 1,
	Playing = 2,
	NotPlay = 3,
}
local freefallState
humanoid.StateChanged:Connect(function(old, new)
	if new == Enum.HumanoidStateType.Freefall then
		freefallState = State.ToPlay
		task.wait(1)
		if freefallState == State.ToPlay then
			freefallState = State.Playing
			print("play")
			--kickAnimationTrack:Play()
		end
	elseif old == Enum.HumanoidStateType.Freefall then
		if freefallState == State.ToPlay then
			freefallState = State.NotPlay
			print("no play")
		elseif freefallState == State.Playing then
			print("stop")
			--kickAnimationTrack:Stop()
		end
	end
end)
1 Like

mm make sure the animationtrack is looped. the code doesnt really have any issues i guess. except for a few that dont really matter

the roblox statetype could also be doing something kinda random. the developer descriptions are vague so i usually create my own states in runservice heartbeat… like activeState = airState which can be checked with Humanoid.FloorMaterial == Enum.Material.Air if you end up trying it

local fallId = 0

humanoid.StateChanged:Connect(function(old, new)
	if new == Enum.HumanoidStateType.Freefall then
		-- save the current fall ID
		local id = fallId
		-- wait 1 second
		task.wait(1)
		-- if the fall ID has changed then exit
		if id ~= fallId then return end
		-- but if the fall ID is still the same then start playing the animation
		kickAnimationTrack:Play()
	else
		-- increment the fall ID (this will stop the animation from playing if the 1 second wait has not finished yet)
		fallId += 1
		-- stop the animation
		kickAnimationTrack:Stop()
	end
end)