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
.