PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

StateChanged

This event fires when the state of the Humanoid is changed.

The humanoid state describes the activity the Humanoid is currently doing. It takes the form of a Enum/HumanoidStateType value.

See also:

  • To get and set the state use Humanoid/GetState and Humanoid/ChangeState
  • Individual states can be disabled using Humanoid/SetStateEnabled
  • As there is no idle humanoid state, you should instead use the Humanoid/Running event or listen to the Humanoid/RootPart|RootPart's BasePart/Velocity to work out when the Humanoid is standing still

Parameters

Name Type Default Description

old

The Humanoid|Humanoid's previous state type

new

The Humanoid|Humanoid's current state type


Code Samples


Jump Cooldown

The following sample will require a one second cooldown after a Humanoid has landed before it is able to jump again.

To try this sample, place it inside a LocalScript in StarterCharacterScripts|StarterPlayer.StarterCharacterScripts.

local character = script.Parent

local JUMP_DEBOUNCE = 1

local humanoid = character:WaitForChild("Humanoid")

local isJumping = false
humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping then
		if not isJumping then
			isJumping = true
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
		end
	elseif newState == Enum.HumanoidStateType.Landed then
		if isJumping then
			isJumping = false
			wait(JUMP_DEBOUNCE)
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
		end
	end
end)

Jumping Particles

Emits particles from the Player|Player's Player/Character when they jump.

To try this code sample, place it inside a LocalScript parented to StarterCharacterScripts|StarterPlayer.StarterCharacterScripts.

local character = script.Parent

local primaryPart = character.PrimaryPart

-- create particles
local particles = Instance.new("ParticleEmitter")
particles.Size = NumberSequence.new(1)
particles.Transparency = NumberSequence.new(0, 1)
particles.Acceleration = Vector3.new(0, -10, 0)
particles.Lifetime = NumberRange.new(1)
particles.Rate = 20
particles.EmissionDirection = Enum.NormalId.Back 
particles.Enabled = false
particles.Parent = primaryPart

local humanoid = character:WaitForChild("Humanoid")

local isJumping = false

-- listen to humanoid state
humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping then
		if not isJumping then
			isJumping = true
			particles.Enabled = true
		end
	elseif newState == Enum.HumanoidStateType.Landed then
		if isJumping then
			isJumping = false
			particles.Enabled = false
		end
	end
end)