Press a key to play animation

From this you'll learn how to play an animation when pressing a key

by MrCat4213

Author Avatar

Some brief explaining before we start We are going to use UIS(UserInputService) for this script, so that means we have to make a local script and put it in StarterGui. You can name your script whatever you want since it wont change how it works. There is also a way to do this with KeyDown but I prefer to use UIS.

Variables First, lets set up our variables to detect our player and his character:

local player = game.Players.LocalPlayer or  game.Players.PlayerAdded:Wait()
local character = game.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

After we set up the variables for the player and the character lets make a simple variable for UIS:

local uis = game:GetService("UserInputService")

After we've done that, lets set up our animation! Heres how we do it:

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://TheIdOfYourAnimationHere"

The Function Now that we're done with all of the variables for the script, lets make the actual script!

 function KeyPress(inputObject, gameProcessEvent)
if inputObject.KeyCode == Enum.KeyCode.E then
humanoid:LoadAnimation(anim):Play()
end
end

This function will the UIS activate and preform the task we gave it once we press the button we want, I simply chose E but you can change anything that comes after "Enum.KeyCode." to what you desire.

Almost there! Now that you've read through this you know that how to set up a very simple function and simple variables, all together give you this

local player = game.Players.LocalPlayer or  game.Players.PlayerAdded:Wait()
local character = game.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://TheIdOfYourAnimationHere"

function KeyPress(inputObject, gameProcessEvent)
if inputObject.KeyCode == Enum.KeyCode.E then
humanoid:LoadAnimation(anim):Play()
end
end

This script will work completely fine, but theres a problem. In order for the UIS to activate we must put a connect event for our UIS so it'll actually work. to do this simply add this to the end of your script:

uis.InputBegan:Connect(KeyPress)

so all together would be

local player = game.Players.LocalPlayer or  game.Players.PlayerAdded:Wait()
local character = game.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://TheIdOfYourAnimationHere"

function KeyPress(inputObject, gameProcessEvent)
if inputObject.KeyCode == Enum.KeyCode.E then
humanoid:LoadAnimation(anim):Play()
end
end

uis.InputBegan:Connect(KeyPress)

Good job!

You've done it! You made a UIS script to make an animation play!

Cooldowns One small thing you can do, this can be used for stuff like attack cooldowns. To add a cooldown to our animation we use Debounce. To set up the debounce you add another variable to our variable collection:

local debounce = false

Now that you've made the variable we make some small changes to the function:

function KeyPress(inputObject, gameProcessEvent)
if not debounce then
if inputObject.KeyCode == Enum.KeyCode.E then
debounce = true
humanoid:LoadAnimation(anim):Play()
wait(3) -- I set up the cooldown to be 3 seconds, you can change it to your liking
debounce = false
end
end
end

so all of this entire script together would be

local player = game.Players.LocalPlayer or  game.Players.PlayerAdded:Wait()
local character = game.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://TheIdOfYourAnimationHere"
local debounce = false

function KeyPress(inputObject, gameProcessEvent)
if not debounce then
if inputObject.KeyCode == Enum.KeyCode.E then
debounce = true
humanoid:LoadAnimation(anim):Play()
wait(3) -- I set up the cooldown to be 3 seconds, you can change it to your liking
debounce = false
end
end
end
uis.InputBegan:Connect(KeyPress)

Thank you for reading this tutorial, hope I helped you!

View in-game to comment, award, and more!