Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to use a Animation on KeyDown?

Asked by
kheng1 10
8 years ago
1local player = game.Players.LocalPlayer
2local mouse = player:GetMouse()
3 
4function onkey(key)
5    if key == "b" -- change to whatever key you want to trigger the function--
6then       
7        player.Character.Humanoid.WalkSpeed = 16 * 2.5
8            endend
9mouse.KeyDown:connect(onkey)

Now i want to use a Animation how to do that when i press B?

1 answer

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

To play an animation when a key is pressed you would need to know three things. Animation Track, Animation Instance, and User Input Service. Now to load an animation onto the character you must first create the animation and make an animation track. This would be done by creating an animation instance and using the method :LoadAnimation() on the character's humanoid. This is how that would look:

1local player = game.Players.LocalPlayer
2repeat wait() until player.Character
3local character = player.Character
4 
5local animation = Instance.new("Animation")
6animation.AnimationId = "Roblox Animation Asset ID Here"
7local track = character:FindFirstChild("Humanoid"):LoadAnimation(animation)

Now we have the track defined, but we want to use this animation whenever the player presses a certain key. Now we will use the user input service. The userinputservice is used to fire events and functions whenever the player interacts with their mouse,keyboard,touchscreen, ect. So to do this we first must get the service using the :GetService() method.

1local UIS = game:GetService("UserInputService")

Now that we have the service we need to run our code whenever the player hit's a specific key. To do this we use the InputBegan listener. This will wait until a player has given the game any type of input, whether is be with the mouse, the keyboard, or a touch screen. We will connect this listener to a function whenever the event is fired. The InputBegan event passes 2 arguments, game processed event and the input. A game processed event is anything processed by the game itself, for example chatting in the chat box.

1UIS.InputBegan:connect(function(input, proc)
2    if not proc then -- So that it wont fire when the player is chatting
3        if input.KeyCode == Enum.KeyCode.B then --We will see if the key pushed is "B"
4            track:Play()
5            character.Humanoid.WalkSpeed = 16 * 2.5
6        end
7    end
8end)

So now lets put it all together into one chunk of code. We also will add in the debounce so the player doesn't spam it an potentially create lag and disorder

01local player = game.Players.LocalPlayer
02repeat wait() until player.Character
03local character = player.Character
04local deb = false -- our debounce
05local animation = Instance.new("Animation")
06animation.AnimationId = "Roblox Animation Asset ID Here"
07local track = character:FindFirstChild("Humanoid"):LoadAnimation(animation)
08local UIS = game:GetService("UserInputService")
09UIS.InputBegan:connect(function(input,proc)
10    if not proc then -- So that it wont fire when the player is chatting
11        if input.KeyCode == Enum.KeyCode.B and deb == false then
12            deb = true
13            track:Play()
14            character.Humanoid.WalkSpeed = 16 * 2.5
15            wait(1)
16            deb = false
17        end
18    end
19end)

Hope I Helped ~~Koolkid

Ad

Answer this question