StateChanged script won't fire elseif

hum.StateChanged:Connect(function(old, new)
	print("StateChanged says "..tostring(hum:GetState()))
	if new ~= Enum.HumanoidStateType.Running or Enum.HumanoidStateType.RunningNoPhysics then
		anim.Priority = Enum.AnimationPriority.Core
		print(anim.Priority)
	elseif new == Enum.HumanoidStateType.Running or Enum.HumanoidStateType.RunningNoPhysics then
		anim.Priority = Enum.AnimationPriority.Action
		print(anim.Priority)
	end
end)

The first part always fires, regardless of whether or not the Humanoid is in the Running or RunningNoPhysics state. Why?

It’s because the part after the or is done incorrectly,

if new ~= Enum.HumanoidStateType.Running or Enum.HumanoidStateType.RunningNoPhysics then

Should be

if new ~= Enum.HumanoidStateType.Running or new ~= Enum.HumanoidStateType.RunningNoPhysics then

Same goes for the elseif as the same mistake is done there, having Enum.HumanoidStateType.RunningNoPhysics by itself will always return true for the script, so it will always go through with the if portion, you need to compare that as well with something, in this case, the new state of the humanoid

Not sure that is how you wanted the if statement, but understanding that you always need to ocmpare should tell you how you can fix the issue

1 Like

Using this still did not work.

hum.StateChanged:Connect(function(old, new)
	print("StateChanged says "..tostring(hum:GetState()))
	if new ~= Enum.HumanoidStateType.Running or new ~= Enum.HumanoidStateType.RunningNoPhysics then
		print("if was fired")
		anim.Priority = Enum.AnimationPriority.Core
		print(anim.Priority)
	elseif new == Enum.HumanoidStateType.Running or new == Enum.HumanoidStateType.RunningNoPhysics then
		print("elseif was fired")
		anim.Priority = Enum.AnimationPriority.Action
		print(anim.Priority)
	end
end)

Every time it runs, regardless of what happens to the humanoid state, still prints if was fired.

I think in that case and has to be used?

if new ~= Enum.HumanoidStateType.Running and new ~= Enum.HumanoidStateType.RunningNoPhysics then
1 Like