I want to add animations to my game, but I need permanent keybinds. Im not sure how to script these though, any tips would be extremely helpful.
How would I make a keybind script for an animation?
2 answers
Use UserInputService. UserInputService allows you to use keybinds to perform functions (such as loading animations.) I'll show you a part of it:
--This has to be a local script due to server scripts not accepting UIS
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input,GPE) --GPE means game processed.
if GPE then return end --If the player is e.g. chatting, then do nothing.
if input.KeyCode = Enum.KeyCode.E then
print("Player pressed E")
end
end)
to include animations, I suggest researching about Animation:LoadAnimation and Animation:Play().
Sources: Devking's UIS Video
Hey MeOwnYouNoobs,
To do this, it is actually quite simple to do. We will be using UserInputService to pull this off. First things first, we are going to need to set up a way for the Client to communicate with the Server. We do this because UserInputService is, for obvious reasons, required to be performed in a local script. Before we get started, I suggest you read the following wiki page to get a better understanding of the class service we will be using.
developer.roblox.com/en-us/api-reference/class/UserInputService)
Make sure to add in a RemoteEvent into replicated storage. This is key, otherwise, we will not have a gateway for the client to communicate with the server. Also, make sure to read this wiki post about Remote Events and Functions
https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events
First things first: Define Variables
Type the following code into the start of your LocalScript. I've left comments most lines to help you out.
local UserInputService = game:GetService("UserInputService") -- gets UserInputService
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- gets ReplicatedStorage
local RemoteEvent = ReplicatedStorage.RemoteEvent -- change name to name of remote
local players = game:GetService("Players") -- get's the service class of players
local plr = players.localplayer -- defines the local/client player
local humanoid = plr.Character.Humanoid -- defines the humanoid of the local/client player
Once you've done that, we can finally get started with the Function
UserInputService.InputBegan:Connect(function(input, gameProcessed) -- Detects user input
end) -- ends the function. Stops it.
Now we can start telling it what we want it to do when the input is detected.
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.Keycode == Enum.KeyCode.Q -- change Q to what ever letter you want
end
end
end
end)
Okay, we added quite a bit to the code just then, so let's do a quick recap. First off, what is
if gameProcessed then
as you can see at the top of your function we have two arguments. Arguments are what is inside the brackets. If you are not familiar with these I suggest you go and read up on it at the wiki. Regardless, when doing functions with UserInputService the first two arguments will always be input and game processed. You can name them whatever you want but they will always return the same thing. I am still unfamiliar what input is, but you can use it to define more specific things such as the KeyCode or the UserInputType. The second argument is game processed which will return either true or false depending on if the input type is a game processed event or not. This is really useful for detecting if the player is pressing the key when they are chatting or if they are actually playing the game. If they are in chat, it would not be a game processed event so it would return false, otherwise true. So we check that first because we don't want to fire the animations when they are talking in chat, do we? After that, we check if the input type came from a keyboard and then on the next line we check if the correct key was pressed. The correct key is your choice, I set it to Q as an example. Make sure to use capitals, I think. If you research more about UserInputService you can see a list of all the different input types it supports.
Now, we can fire an event to the server letting the server know that the specified button was pressed, and it's time to play that animation.
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.Keycode == Enum.KeyCode.Q -- change Q to what ever letter you want
RemoteEvent:FireServer() -- sends an activation to the server through the remote event
end
end
end
end)
Next, let's hook up the server to that remote event
Insert a Script to ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.RemoteEvent
local Animation = Instance.new("Animation") -- Creates a new animation
Animation.AnimationId = "rbxassetid://000000000" -- add the animation ID here.
local function playAnimation(plr)
end
RemoteEvent.OnFireServer:Connect(playAnimation) -- this line detects when the client fires the server through the remote event, and then immediately connects the play animation function that we created to start running what is inside of the function.
You might be wondering why I put (plr) inside the parameters of playAnimation. This is because when you fire from the client to the server it automatically passes the player object through that remote event, you can type any words inside those brackets, but the first argument will always be the player object. This is good because now we can specify what player we want to play the animation for.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.RemoteEvent
local Animation = Instance.new("Animation") -- Creates a new animation
Animation.AnimationId = "rbxassetid://000000000" -- add the animation ID here.
local function playAnimation(plr)
local animationTrack = plr.Character:FindFirstChild("Humanoid"):LoadAnimation(Animation)
animationTrack:Play()
end
RemoteEvent.OnFireServer:Connect(playAnimation)
Using the player object argument, we were able to define the player that pressed the key and get their characters humanoid. Which allows us to then load the animation we previously stated in our variables.
After that, you are done. By the way, with UserInputService you can also detect when the input has stopped, along with its many other features (read the wiki). Here is how to detect that. Remember this should also be performed in the Local Script
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.Keycode == Enum.KeyCode.Q
RemoteEvent:FireServer()
end
end
end
end)
Visit these wiki pages on UserInputService:
https://developer.roblox.com/en-us/api-reference/class/UserInputService
https://developer.roblox.com/en-us/api-reference/class/Animation https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events
Also, make sure to upvote if this helped :)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
I left a more detailed response below.