Animation won’t play on KeyBind event

I have created lots of animation scripts in the past, but haven’t worked on them in a while. I tried one yesterday and it doesn’t work. When the player presses ‘G’, the animation doesn’t play, when it is supposed to. Any thoughts on this?

The Script

`local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService(“UserInputService”)

local AnimationID = – This is my animation rbxasset:// thing (filled in correctly)
local Debounce = true
local key = ‘G’

UserInputService.InPutBegan:Connect(function(Input, IsTyping)
if IsTyping then return end
if Input.KeyCode = Enum.KeyCode[key]and Debounce == true then
Debounce = false
local Animation = Instance.new(“Animation”)
Animation.AnimationId = Animation Id
local LoadAnimation = Humanoid:LoadAnimation(Animation)
wait(1)
Animation:Destroy()
Debounce = true
end
end)`

How do I make sure that when a specific key is pressed, the animation plays?

Many thanks,

Crrustyy.

I am running my PC now, I will get the script ready

You have to use a localscript if you want to detect a player’s inputs, and for detecting inputs you’ll need to know UserInputService.
This is an example I just made:

local UserInputService = game:GetService("UserInputService")
local animation = -- the animation directory
local humanoid = -- the humanoid directory

UserInputService.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.G then
        local animationTrack = humanoid:LoadAnimation(animation)
        wait()
        animationTrack:Play()
    end
end)

I haven’t tested it but it should work.

1 Like

Can you show the code that you’ve used?
Also, make sure the AnimationPriority is set to Action (preferably for this).
More info here: AnimationPriority | Documentation - Roblox Creator Hub

2 Likes

Should I include a debounce or is it unnecessary?

If it’s something that you need to prevent spam, yes. I would recommend using a debounce.

1 Like

I have put the animation script in the post now. Is there something wrong with it?

This should be
InputBegan:Connect()

Also, I’m not 100% sure if you need a debounce on it, you may need to see if it can be spammed.

so is there any need for this lot?

There are 2 problems with that script,

.InPutBegan should be .InputBegan

You need to put LoadAnimation:Play() so the animation plays.

1 Like

Yes, I just didn’t include that in the small fix I provided. Input, quite plainly is the input that the player inputs (e.g. a key press). IsTyping makes sure that the player doesn’t press G when typing in a GUI instead, for example, in chat as this would lead to unintentional usage.

If a player types “good morning” and the script checks for the G keycode, and you don’t check the IsTyping, then they’ll activate the animation.

2 Likes

Yes, that line is needed to find the Input and if that input was processed by the game.

2 Likes

Is that before the wait(1)? . .

1 Like

I think so. Although, I don’t know if it changes anything when animation gets deleted after being loaded into Humanoid.

1 Like

Input says “Play is not a valid member of Animation”. How do I solve this?

Make sure you use :Play(), not .Play()

You need to put

LoadAnimation:Play()

But you probably did it like this:

Animation.Play
2 Likes

Thanks! turns out I put Animation:Play() not LoadAnimation:Play().

1 Like

Hmm, it still doesn’t work. If I made an R6 Animation, and tried to make a rthro player do the animation, would it work?
This is the current situation.

1 Like

I doubt it. This is why R6 and R15 animations weren’t compatible with each other. For example, you couldn’t use an R6 animation on an R15 character, and vice versa.

2 Likes