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
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.
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”.
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.
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.