Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to use a Animation on KeyDown?

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

function onkey(key)
	if key == "b" -- change to whatever key you want to trigger the function--
then		
		player.Character.Humanoid.WalkSpeed = 16 * 2.5
			endend
mouse.KeyDown:connect(onkey)

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

1 answer

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:

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local character = player.Character

local animation = Instance.new("Animation")
animation.AnimationId = "Roblox Animation Asset ID Here"
local 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.

local 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.

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

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

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local character = player.Character
local deb = false -- our debounce 
local animation = Instance.new("Animation")
animation.AnimationId = "Roblox Animation Asset ID Here"
local track = character:FindFirstChild("Humanoid"):LoadAnimation(animation)
local UIS = game:GetService("UserInputService")
UIS.InputBegan:connect(function(input,proc)
	if not proc then -- So that it wont fire when the player is chatting
		if input.KeyCode == Enum.KeyCode.B and deb == false then
			deb = true
			track:Play()
			character.Humanoid.WalkSpeed = 16 * 2.5
			wait(1)
			deb = false
		end
	end
end)

Hope I Helped ~~Koolkid

This discussion is preserved as it was. New answers, comments, and votes are closed.