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