Animation on key. (W for animation help)

Hello, So here’s my question.

How can I play a walking animation depending on the key you’re pressing?

For example, if I push W it plays an animation, but if I push A it plays another animation.

If you have any additional resources I could use, please let me know! Thanks!

3 Likes

What you could do instead of detecting indviudal key presses is use the Humnoid.Running event to check if the player is walking and then play/stop the animation depending on the speed. Humanoid | Documentation - Roblox Creator Hub

Is there a way to detect which way the player is walking?

you could check the orientation. Although if you want different aniamtions for each direction then key presses would be better. Why would you need seperate animations for different walking directions though?

For example, if you play the game Soul Shatters you will know what I mean, But basically if you press A it plays an animation of you walking left, same with Up, Left, Right, and Down.

Is it the same animation for moving all the directions?

Nope, I have 4 different animations, but my idea is it stops all the other animations and plays the one corresponding to the key.

I still dont get what you are trying to achieve. Can you send an example video?

Ok, so.

lets say you are shift locking and you push D.

It will then play an animation.

And each walking key, A W S D will have their own animations for when you press them.

if that’s not worded right, pls let me know!

You can use UserInputService to unlock such a feature for the animation when pressing W or N on a keyboard and such, you can read the article here for more info: UserInputService.

However, you can set it up like this:

local UserinputService = game:GetService("UserInputService")

local function onInBegan(input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.D then
        --Or:
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        --Put the actions that you need to achieve here for animations.
        print("In!")
        end
    end
end

UserinputService.InputBegan:Connect(onInBegan)

What are we doing here? Well, we’re getting the service of UserInputService and setting it up like a function, then we’ll add an if / then statement to get a set of more functions and animations (like your solution) and then end the connection as typed above so it’ll work as intended for printing and such.

You can use Enum for buttons like MouseButton1 or Keyboard, KeyCode is most likely used for buttons like “A” and “E”.

5 Likes

Thank you for this explanation!

Although, I still have a question.
How would I end all current playing animations? Like if I let go of D and the animation stops? Thank you!

1 Like

As long as the animation isn’t a looping animation, pressing the KeyCode value isn’t like holding as you might need a for i or while true do loop(s) to achieve something like that, not entirely sure as I’ve never tried it out…

KeyCode just a single press and another tip: UserInputService is Client-sided but using a remote will make it server-sided.

1 Like

You can easily use Humanoid.MoveDirection to check which way they are moving. Whenever the character walks MoveDirection is changed.

1 Like

Ok! Thanks!
(30 Digits, or whatever.)

And how could I detect which way the player would be walking?

All I really need to know is, how would I stop an animation on a key?

If you print a humanoid’s move direction it returns x, y, z values, so you can check if the x, y or z is ~= 0 and if it is not 0 then you can tell that the humanoid is moving.

1 Like

Assuming you know how to play animations. Wouldn’t you just do Animation:Stop() on the animation? The same way you would do Animation:Play()

This is a model for you to use. Put this in starter pack key code animation script - Roblox

You can mark a reply a solution so people don’t have to like write a 6 page essay and realize “uh oh i wasted 22 minutes of my life here we go again”.

1 Like