Humanoid:ChangeState stops working after a while

Hi,

I am trying to make NPCs jump continuously for a game mode. I wrote the script below (in the simplest form I could write to identify the issue). I’ve added a character (NPC) in Workspace and put the script on the character.

It works at first, but after some repetitions the NPC stops jumping. Any help will be appreciated!

local humanoid = script.Parent:WaitForChild("Humanoid")

while true do
	wait(2)
	humanoid:ChangeState(Enum.HumanoidStateType.Jumping)	
end

Screen Recording.wmv (2.1 MB)

Can’t you just set Humanoid.Jump to true instead? It’s the same thing I think
That could solve the problem

local humanoid = script.Parent:WaitForChild("Humanoid")

while true do
	wait(2)
	humanoid.Jump = true	
end

Thanks for the suggestion! I’ve tried to do that, but humanoid.Jump = true does not play the jumping animation, and I need the NPC to jump with the animation. So, I wrote the simplest script I can think of to run the jumping animation using humanoid.Jump = true, but the same thing happens. It runs correctly for a while, then the animation stops although the jumping continues (notice the arms later in the video). Any help in making the continuous jumping animation run correctly would be appreciated!

local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid.Animator
local jumpAnimation = animator:LoadAnimation(script.Parent.Animate.jump.JumpAnim)

humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping then
		jumpAnimation:Play()
	elseif newState == Enum.HumanoidStateType.Landed then
		if(jumpAnimation.IsPlaying) then jumpAnimation:Stop() end
	end
end)

while true  do
	wait(2)
	--humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	humanoid.Jump = true
end

Screen Recording2.wmv (2.0 MB)

I’ve done a little research and it seems like it’s not the fault of your script. Turns out the StateChanged event can fire inconsistently and it is expected behavior. Source (read ContextLost’s post)
The serversided Animation script in a verified model by roblox I found listens for .Jumping event and waits 0.3 seconds to stop the jump anim instead of waiting for Landed state. Try doing that instead. Or doing what ContextLost suggested in his post I linked.

1 Like

Thanks a lot! Based on your suggestion I have removed all state dependent code as follows and it works reliably now.

local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid.Animator
local jumpAnimation = animator:LoadAnimation(script.Parent.Animate.jump.JumpAnim)

--humanoid.StateChanged:Connect(function(oldState, newState)
--	if newState == Enum.HumanoidStateType.Jumping then
--		jumpAnimation:Play()
--		wait(1)
--		jumpAnimation:Stop()
--	elseif newState == Enum.HumanoidStateType.Landed then
--		if(jumpAnimation.IsPlaying) then jumpAnimation:Stop() end
--	end
--end)

function OnJumping()
	jumpAnimation:Play()
	wait(0.5)
	jumpAnimation:Stop()
end

while true  do
	wait(2)
	--humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	humanoid.Jump = true
	local c = coroutine.create(OnJumping)
	coroutine.resume(c)
end
2 Likes