Keybind animation

I want to create a keybind animation for example when you press “b” the animation plays and when you press “b” again it stops. I’ve used keybind animation scripts to make an animation be able to be played when you press “b” but not stop again when you press “b” again.

Anybody who knows what to do please share it with me, Thanks.

3 Likes

You could do an on key event,

local TheKeyYouWant = "B"
local Player = game.Players.LocalPlayer
local Character = Player.Character
local AnimationID = "3357098976"
local CharAnimation
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode[TheKeyYouWant] then
  animation()
end
end)
function animation()
  if Character then
    local UseLessAnimation = Character:FindFirstChild("AnimationCharacter")
    if CharAnimation then
      CharAnimation:Stop()
    end
    if UseLessAnimation then
      if UseLessAnimation.AnimationId=="rbxassetid://"..AnimationID then
        UseLessAnimation:Destroy()
        return
      end
      UseLessAnimation:Destroy()
    end
    local Animation =Instance.new("Animation",Character)
    Animation.Name= "AnimationCharacter"
    Animation.AnimationId = "rbxassetid://"..AnimationID
    CharAnimation= Character.Humanoid:LoadAnimation(Animation)
    CharAnimation:Play()
  end
end

You could also take that and add a couple of elseif statements to play different animations!

local Player = game.Players.LocalPlayer
local Character = Player.Character
local CharAnimation
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.B then
  animation("yourID")
elseif inputObject.KeyCode == Enum.KeyCode.X then
  animation("yourID")
end
end)
function animation(AnimationID)
  if Character then
    local UseLessAnimation = Character:FindFirstChild("AnimationCharacter")
    if CharAnimation then
      CharAnimation:Stop()
    end
    if UseLessAnimation then
      if UseLessAnimation.AnimationId == "rbxassetid://"..AnimationID then
        UseLessAnimation:Destroy()
        return
      end
      UseLessAnimation:Destroy()
    end
    local Animation =Instance.new("Animation",Character)
    Animation.Name= "AnimationCharacter"
    Animation.AnimationId = "rbxassetid://"..AnimationID
    CharAnimation= Character.Humanoid:LoadAnimation(Animation)
    CharAnimation:Play()
  end
end

This is pretty simple, and you can customize it, even creating buttons instead of key events.
Hope I helped!
(Had to edit a few times because there where a few errors in the scripts)

Example of this working and being used;
https://i.gyazo.com/b528f38437fd8d755a8b142e0d25d47a.mp4

19 Likes

Sorry, I’m a tad stupid. How would you implement this via script? Local script in starterpack?

1 Like

VIA LocalScript in StarterGui or StarterPack, yes.
And don’t worry, you aren’t stupid. We learn new things everyday. :smiley:

4 Likes

Thank you so much man, I’m surprised I couldn’t find anybody else who asked about this. Hopefully now that I’ve asked this other people can now do it. Again, thanks a lot mate.

5 Likes

Anytime, glad I could help! :slight_smile:

1 Like

I’ve made a similar script in a game I work on. Since you have an answer I won’t post it here, but I can send you it if you want a table based way of doing it that might make it easier to customize.

Sorry to re-open this thread but I noticed at a later date that when you type and you press that button it plays the animation, this is quite inconvenient (as you can guess).

I don’t like asking for help for something so simple but something so simple can sometimes make it look flawed.

Nevermind: How to Disable Keybind Animations While Chatting

3 Likes