How Do I Make Keybind Animations?

Hey everyone! I’m new to Roblox scripting & I was wondering if you could help me with some code.

I’m working on a game with some friends & I want to code some Keybind Animations. For example, if you press ‘f’ then a punching animation plays only once & then ends.

I’ve tried looking at some videos on YouTube for some ideas on how to code but every tutorial seem outdated.

Any help would be appreciated, thanks!

2 Likes

Hey there, Ryann. First off you need UserInputService. I’ll attatch the code that I did:

local UserInputService = game:GetService(“UserInputService”)

UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
animation:Play()
end
end

2 Likes

Let me know if you need help declaring where your animations should be. Make sure your animation priorities are set to either action or idle depending on the animation.

1 Like

I’m completely new to coding so I have no idea what to do. Do I need to make a local script in StarterPlayer or something? I have my animations done I just need to code them.

Yes, so here:

You put a local script inside of StarterCharacterScripts, and to declare your animation,

local UserInputService = game:GetService(“UserInputService”)
local debounce = true
local character = script.Parent
local humanoid = character:WaitForChild(“Humanoid”)
local animator = humanoid:FindFirstChild(“Animator”)
local animation1 = Instance.new(“Animation”)
animation1.AnimationId = “rbxassetid://6401325016” --Insert your ID
local animationTrack1 = animator:LoadAnimation(animation1)

UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
if debounce then
debounce = false
animationTrack1:Play()
wait(animationLength)
debounce = true
end
end

2 Likes

Is it supposed to look like this? Also it doesn’t work. image

I updated my code. Also, for animationLength, put like 1 for now. Other than that, if you copy my new code, and make sure you put your animationId in, then it should work.

1 Like

It still doesn’t work. Here’s the code:

image

That’s a perfected code to me. Try adjusting your animation’s priority so that it’s NOT CORE OR MOVEMENT. It needs to be Action or IDLE. Make sure the animation belongs to you as well, you cannot take animations from other people. Roblox only allows animations that are for you.

1 Like

Alright I’ll have a look & get back to you. Thanks for the help! :slight_smile:

1 Like

Doesn’t work still. I’ve set the animation as Action but it doesn’t do anything in-game. image

Also, it keeps giving me this error.

image

Add a “end)” at the bottom of your code.

1 Like

Good news! It works in studio but not in-game. I published the game 2-3 times & closed the servers but it still doesn’t work. Any ideas?

If you just made the animation give it time, if you still run into the issue, just re-export it. Other than that, that’s all I got.

1 Like

Alright no worries, thanks for the help! I really appreciate it :slight_smile:

1 Like

Since Wario hasn’t gone through the effort of explaining what any of his code does, I will provide a new and more simple method.

For this I’d recommend looking up Dictionaries, Enums, UserInputService, Animation instances, AnimationTracks, the Animator instance, and the player instance. You can look all of these up on the DeveloperHub (https://developer.roblox.com/). You will need to be comfortable with lua syntax to do something like this.

First you want to create a dictionary that holds the KeyCode Enum and its respective animation instance. A dictionary is similar to an array but values are not indexed by integers, they are indexed by a key.

local Example = {
    [Key here, it is usually a string, number or Enum] = value (in your case value will be the animation instance)
}

You can access specific values in dictionaries via their key Dictionary[key].

Okay awesome, you know what a dictionary is. So now you can create a dictionary that holds your animations and you can assign a KeyCode Enum to them as the key.

local AnimationKeys = {
    [Enum.KeyCode.One] = script.Animation1, -- you will change the key to whatever you need the button to be and you will change the value to your animation instance.
}

Okay so you have your dictionary, now you need these animations to play when the player presses a button. This is where UserInputService comes in, I’d recommend reading up on UserInputService.InputBegan as it is something you will use frequently in other scripts.


UserInputService.InputBegan:Connect(function(input)

end)

input is passed as the first argument of InputBegan, it is used for checking UserInputType or KeyCode, in your case KeyCode. You can see what player the key pressed with input.KeyCode, this will give you the KeyCode Enum of the key pressed.

Since you have the KeyCodes as keys in your dictionary you can simply check if the KeyCode is a key in your dictionary.

UserInputService.InputBegan:Connect(function(input)
    local animationToPlay = yourDictionary[input.KeyCode]

    if animationToPlay then
        -- valid input
    end
end)

Now you need to know about AnimationTracks and the Animator instance. I recommend looking up the Animator instance and AnimationTracks on the Developer Hub as they are pretty hard to explain in a post like this. In your case you probably just need to play the animation without adjusting speed or anything so to play the animation you can do Animator:LoadAnimation(animationInstance):Play().

You will already have your animation instance from the animationToPlay variable. To get the animator instance you need to get the players character and then get its Humanoid. Under the Humanoid there will be the Animator instance. Player.Character.Humanoid.Animator.

2 Likes