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

ChangeState

This function causes the Humanoid to enter the given Enum/HumanoidStateType.

The humanoid state describes the activity the Humanoid is currently doing.

You should check the page for Enum/HumanoidStateType for more information on what particular states do as some may have misleading names. For example, running describes a state where the Humanoid|Humanoid's legs are on the ground, including when stationary

Due to the default behavior of the Humanoid some states will automatically be changed when set to. Below are some examples of this:

  • Setting the state to 'Swimming’ when the Humanoid is not in the water will lead to it being automatically set to ‘GettingUp’
  • As it is unused, setting the state to ‘PlatformStanding’ will lead to it being automatically set to ‘Running’

See also

  • To enable or disable a paticular state use Humanoid/SetStateEnabled
  • To get the current state use Humanoid/GetState

Parameters

Name Type Default Description
None

The Enum/HumanoidStateType that the Humanoid is to perform

Returns

Return Type Summary

Code Samples


This code, when placed inside a LocalScript in StarterCharacterScripts|StarterPlayer.StarterCharacterScripts will allow the player’s characters to perform a double jump.

local UserInputService = game:GetService("UserInputService")
local character = script.Parent

local humanoid = character:WaitForChild("Humanoid")

local doubleJumpEnabled = false 

humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping then
		if not doubleJumpEnabled then 
			wait(.2)
			if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
				doubleJumpEnabled = true 
			end
		end
	elseif newState == Enum.HumanoidStateType.Landed then	
		doubleJumpEnabled = false
	end
end)

UserInputService.InputBegan:Connect(function(inputObject)
	if inputObject.KeyCode == Enum.KeyCode.Space then
		if doubleJumpEnabled then
			if humanoid:GetState() ~= Enum.HumanoidStateType.Jumping then
				humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
				spawn(function()
					doubleJumpEnabled = false 
				end)
			end
		end		
	end
end)