:GetState() not working

Hey. I made a sword tool animations, in different states, it will play a different animation.
I used :GetState() to detect and landing, but it doesn’t work.
Here is my code:

wait(1)
local tool = script.Parent.Parent;
local userInputService = game:GetService("UserInputService");
local tweenService = game:GetService("TweenService");
local localPlayer = game:GetService("Players").LocalPlayer;
local humanoid = localPlayer.Character:FindFirstChildOfClass("Humanoid");
local remoteEvent = script.Parent:WaitForChild("Remote");
local isRunning = false;
local currentAnimation = nil;
local isEquipped = false;

local animations = {
	Idle = game:GetService("ReplicatedStorage").SwordAnimations.Idle;
	Walking = game:GetService("ReplicatedStorage").SwordAnimations.Walking;
	Running = game:GetService("ReplicatedStorage").SwordAnimations.Running;
	Jumping = game:GetService("ReplicatedStorage").SwordAnimations.Jumping;
	Fall = game:GetService("ReplicatedStorage").SwordAnimations.Fall;
};

local c = coroutine.wrap(function()
	while wait() do	
		if not isEquipped then remoteEvent:FireServer("Stop", nil) coroutine.yield() end

		if localPlayer.IsDashing.Value then
			currentAnimation = "Dashing"
		end

		if humanoid.MoveDirection == Vector3.new() and not localPlayer.IsDashing.Value then
			--Idle
			if currentAnimation ~= animations.Idle and not humanoid:GetState() == Enum.HumanoidStateType.Jumping and not localPlayer.IsDashing.Value then
				remoteEvent:FireServer("Play", animations.Idle)
				currentAnimation = animations.Idle
			end
		elseif humanoid.MoveDirection ~= Vector3.new() and not isRunning and not humanoid:GetState() == Enum.HumanoidStateType.Jumping and not localPlayer.IsDashing.Value then
			--Walking
			if currentAnimation ~= animations.Walking then
				remoteEvent:FireServer("Play", animations.Walking)
				currentAnimation = animations.Walking
			end
		elseif humanoid.MoveDirection ~= Vector3.new() and isRunning and not humanoid:GetState() == Enum.HumanoidStateType.Jumping and not localPlayer.IsDashing.Value then
			--Running
			if currentAnimation ~= animations.Running and not localPlayer.IsDashing.Value and not humanoid:GetState() == Enum.HumanoidStateType.Jumping then
				remoteEvent:FireServer("Play", animations.Running)
				currentAnimation = animations.Running
			end
		elseif humanoid:GetState() == Enum.HumanoidStateType.Jumping then
			print("Jumping")
			--Jumping
			if currentAnimation ~= animations.Jumping and not localPlayer.IsDashing.Value then
				remoteEvent:FireServer("Play", animations.Jumping)
				currentAnimation = animations.Jumping
			end
		elseif humanoid:GetState() == Enum.HumanoidStateType.Landed then
			if currentAnimation ~= animations.Fall and not localPlayer.IsDashing.Value then
				humanoid:LoadAnimation(animations.Fall):Play()
				currentAnimation = animations.Fall
			end
		end
	end
end)

function on_Equip()
	isEquipped = true
	c()
	localPlayer.CameraMovementRelative.Value = false
end

function on_Unequip()
	isEquipped = false
	tweenService:Create(workspace.CurrentCamera, TweenInfo.new(.3), {FieldOfView = 70}):Play()
	currentAnimation = nil;
	localPlayer.CameraMovementRelative.Value = true
end;

function input_Began(input, gpe)
	if gpe then return end;
	if not isEquipped then return end;
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = true
		tweenService:Create(workspace.CurrentCamera, TweenInfo.new(.3), {FieldOfView = 90}):Play()
	end
end;

function input_Ended(input, gpe)
	if gpe then return end; 
	if not isEquipped then return end;
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = false
		tweenService:Create(workspace.CurrentCamera, TweenInfo.new(.3), {FieldOfView = 70}):Play()
	end
end;

-- // Connecting the events
tool.Equipped:Connect(on_Equip);
tool.Unequipped:Connect(on_Unequip);
userInputService.InputBegan:Connect(input_Began);
userInputService.InputEnded:Connect(input_Ended)

(Note: Before I used :GetState() it worked PERFECTLY fine)
This is a LocalScript

The problem is not :GetState(). It’s the way you use the not operator. The way not operator works is that it turns the value after it into true or false. If the value is nil or false, it becomes true. Otherwise, it becomes false. When you write

not humanoid:GetState() == Enum.HumanoidStateType.Jumping

not turns the state returned by :GetState() into false and then false is compared to Enum.HumanoidStateType.Jumping. The value of this comparison is never true.

There are two ways to fix this.

not (humanoid:GetState() == Enum.HumanoidStateType.Jumping)

in the above code the comparison is done first and the value is then turned into its opposite with not operator.

humanoid:GetState ~= Enum.HumanoidStateType.Jumping
2 Likes

Can I use this in any case I use not? I didn’t know this.

Use what? The two ways I mentioned for fixing the problem? Yes, you can use those in other situations too. Putting something inside parenthsis always makes it happen before the things around it. And not operator always works the same way, whether you are setting the value of a variable using not or using not in an if statement.

1 Like

The Humanoid touched the ground after a freefall. This state lasts only briefly.

The Humanoid just jumped. (Check Humanoid.Jump ). This state lasts only briefly.

I’d recommend use different states. You could also use events to store when those states happened recent.

You also might want to connect your function to state changed instead of using a loop.

Hey! I am sorry for a late response. Thanking you for your respond, I am right now testing it.

1 Like