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