Why does the wrong animation play when holding any button?

I made this script that plays animation when the player jumps/walks.

The problem is that the when the player holds any button it starts the walk animation.

local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")

local WalkAnim = script:WaitForChild("Walk")
local walkAnimTrack = humanoid.Animator:LoadAnimation(WalkAnim)

local JumpAnim = script:WaitForChild("Jump")
local JumpAnimTrack = humanoid.Animator:LoadAnimation(JumpAnim)

local uis = game:GetService("UserInputService")

function onJumpRequest()
	if not JumpAnimTrack.IsPlaying then   
		JumpAnimTrack:Play()
		JumpAnimTrack:AdjustSpeed(1.5)
	end
end

uis.JumpRequest:Connect(onJumpRequest)

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.W or Enum.KeyCode.S or Enum.KeyCode.D or Enum.KeyCode.A then
			if not walkAnimTrack.IsPlaying then   
				walkAnimTrack:Play()
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.W or Enum.KeyCode.S or Enum.KeyCode.D or Enum.KeyCode.A then
			if uis:IsKeyDown(Enum.KeyCode.S) == false then
				if uis:IsKeyDown(Enum.KeyCode.D) == false then
					if uis:IsKeyDown(Enum.KeyCode.A) == false then
						if uis:IsKeyDown(Enum.KeyCode.W) == false then
							walkAnimTrack:Stop()
						end
					end
				end
			end
		end
	end
end)
2 Likes

I think an ‘elseif’ might works, since it always worked for me. This also might work for the other functions. (This is just an example)

function onJumpRequest()
	if not JumpAnimTrack.IsPlaying then   
		JumpAnimTrack:Play()
		JumpAnimTrack:AdjustSpeed(1.5)
	elseif walkAnimTrack.IsPlaying then
		walkAnimTrack:Stop()
		JumpAnimTrack:Play()
		JumpAnimTrack:AdjustSpeed(1.5)
	end
end
2 Likes

that does solve the jump issue, but it still plays the walk animation whenever I press something else, like the K key.

Maybe I did incorrect syntax on this line? I’m not too familiar with “or”, and it seems like it only accepts W when the “or” statements are removed (although I need SDA of course).

if input.KeyCode == Enum.KeyCode.W or Enum.KeyCode.S or Enum.KeyCode.D or Enum.KeyCode.A then
1 Like

Just a question: Why do you check if the Character has a keyboard, I am pretty sure that Enum.KeyCode also works completely fine

if input.UserInputType == Enum.UserInputType.Keyboard then

for some reason it doesn’t work without, although it may now idk
btw the elseif statement also makes the character stop the walk animation after the jump (so if you are jumping while walking and stop jumping, the walk animation won’t work)

1 Like

Also, it’s really better if you use way more tables, because of that, you can just type it instantly out instead of those if statements with many KeyCodes. I wrote a bit of the improved script myself and asked a bit of GPT to fix it. Hopefully this might work! AND DON’T FORGET PRINTS, they are really useful

local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")

local WalkAnim = script:WaitForChild("Walk")
local walkAnimTrack = humanoid.Animator:LoadAnimation(WalkAnim)

local JumpAnim = script:WaitForChild("Jump")
local jumpAnimTrack = humanoid.Animator:LoadAnimation(JumpAnim)

local uis = game:GetService("UserInputService")

function onJumpRequest()
	if not jumpAnimTrack.IsPlaying then   
		jumpAnimTrack:Play()
		jumpAnimTrack:AdjustSpeed(1.5)
	end
end

uis.JumpRequest:Connect(onJumpRequest)

local movementKeys = {
	Enum.KeyCode.W,
	Enum.KeyCode.S,
	Enum.KeyCode.D,
	Enum.KeyCode.A
}

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		for _, keyCode in pairs(movementKeys) do
			if input.KeyCode == keyCode then
				if not walkAnimTrack.IsPlaying then   
					walkAnimTrack:Play()
				end
				break
			end
		end
	end
end)

uis.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local noMovementKeyPressed = true

		for _, keyCode in pairs(movementKeys) do
			if uis:IsKeyDown(keyCode) then
				noMovementKeyPressed = false
				break
			end
		end

		if noMovementKeyPressed then
			walkAnimTrack:Stop()
		end
	end
end)

And with the elseif statement, I forgot the animation will stop forever, since they do weird stuff

2 Likes

that does work - seems that by using tables, it solves the syntax issues.

However in practice having both animations play at the same time is a bit glitchy because of the physical character (it’s a custom character instead of normal R6, so the legs makes the character shake when walkjumping). Do you have an idea for disallowing the walkanimation to play when the jumpanimation is active?

I click solved because it solved the problem in the title for others to see, but if you want to help me further it would be nice because I’d probably just create a new post later anyway.

The only other problem than the playing both animations at the same time, is that the when you hold W and S at the same time, the walkanimation plays but the character doesn’t move of course. I’m not sure how to define the player’s speed inside the UIS function (just has to check if speed > 0).

2 Likes

I think you dont really need that elseif statement but I think there is a simpler version by just adding an if statement in the function. Change the position of the if statement if needed

function onJumpRequest()
	if not jumpAnimTrack.IsPlaying then   
		jumpAnimTrack:Play()
		jumpAnimTrack:AdjustSpeed(1.5)
		if walkAnimTrack.IsPlaying then
			walkAnimTrack:Stop()
		end
	end
end
3 Likes

doesn’t quite work. When jumping straight, the inputbegan isn’t triggered after the jump ended, becasue you are holding down W while walkjumping.

1 Like

Hmm, then I don’t quite now, maybe try some different things and/or work on it later again

3 Likes

alright I came up with this which works, although it does make the walk animation delayed a bit. I’ve learned enough about this topic for now so I’ll come back to this character animation thing once I’ve learned more general studio/scripting knowledge.

function onJumpRequest()
	if not jumpAnimTrack.IsPlaying then
		walkAnimTrack:Stop()
		jumpAnimTrack:Play()
		jumpAnimTrack:AdjustSpeed(2)
		jumpAnimTrack.Ended:Connect(function()
		if not jumpAnimTrack.IsPlaying then
			for _, keyCode in pairs(movementKeys) do
				if uis:IsKeyDown(keyCode) then
						walkAnimTrack:Play()
					end
				end
			end
		end)
	end
end

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