Humanoid.StateChanged Doesn't Print Out Jumping Accurate

I am trying to detect when a player jumps from the server but the Humanoid.StateChanged is very inaccurate and doesn’t work well.

This is my script :

game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	char:WaitForChild("Humanoid").StateChanged:Connect(function(oldState, newState)
		if newState == Enum.HumanoidStateType.Jumping then
			print("Jumping")
		end
	end)
end)

because StateChanged looks like this

game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()

	char:WaitForChild("Humanoid").StateChanged:Connect(function(oldState, newState)
		if newState == Enum.HumanoidStateType.Jumping or newState == Enum.HumanoidStateType.Freefall and oldState ~= Enum.HumanoidStateType.Jumping then
			print("Jumping")
		end
	end)
end)
2 Likes

Oh Thanks It Works Very Well Now!

That’s a great solution as well! It detects jumps using StateChanged very well

There is also another option and is a good way to handle jump detection if you need more control over it for certain actions:

	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
    --- You can add debounce as this one is quite instantaneous,
    humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
		print("Jumping")		
	  end)

This above snippet doesn’t have a debounce, but you can add one like this one below…

game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")

	local jumpSignalDbName = "jumpSignalDebounce" -- Attribute name

	humanoid:SetAttribute(jumpSignalDbName, false)

	humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
		if humanoid:GetAttribute(jumpSignalDbName) == false then
			humanoid:SetAttribute( jumpSignalDbName, not(humanoid:GetAttribute(jumpSignalDbName)) ) -- inverting the boolean's state, since it was false, it'll be true now

			print("Jumping")

			task.wait(0.5) -- debounce delay
			humanoid:SetAttribute( jumpSignalDbName, not (humanoid:GetAttribute(jumpSignalDbName)) ) -- now we'll invert the true back to false again
		end
	end)

end)

I wrote these and tested and both of these work well.

Please let me know if you have any questions about these approaches, these are just other options or ways of doing it for certain actions in your game!

So, are two of the options I showed the best? It depends. On your game system, the specific mechanic, among others you’re trying to create.

Have a great day!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.