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

How do I make an animation play when I press a key?

I want to make a block animation and I want it to be played when the "Q" is down. This is the script I've made.

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://"
mouse.KeyDown:connect(function(key)
  if key == "Q" then
   local playAnim = humanoid:LoadAnimation(anim)
   playAnim:Play()

    end
end)

The problem is that when I let go of the key, it doesn't interrupt the animation. I also wanted to make it zoom out a little when Q is pressed. Pleas help and thanks.

2 comments

0

Oh my! Your script seems to be unorganized. Can you please edit this post with the code inside of the curly underscores? That way, it would be a LOT more easier to understand.

0

Oh my! Your script seems to be unorganized. Can you please edit this post with the code inside of the curly underscores? That way, it would be a LOT more easier to understand.

1 answer

The primary thing to address is this:

mouse.KeyDown:connect(function(key)

This is deprecated, this sort of thing should be handled by ContextActionService or UserInputService. We'll use ContextActionService as it's convenient for binding input to actions.

local context_action_service = game:GetService("ContextActionService")

function block(action, state, object)

end

context_action_service:BindAction("Block", block, false, Enum.KeyCode.Q)

Let's break down what's going on in case you haven't used ContextActionService before.

The first line simply allows us to use the ContextActionService, without having to constantly write game:GetService("ContextActionService").

The function and bottom line are involved in the actual block. The bottom line shows us using the BindAction function to bind the Q key to the function "block".

The function block is called with 3 parameters: the name of the action (you can see on the bottom line I called it "Block", but it can be anything), the state of the user input (essentially has the input started, or has it ended), and the InputObject, which contains more information about the input.

We only really care about state, because we want to check this to make sure the blocking only happens when the Q key is released. Hence, we're only interested in the UserInputState End:

local context_action_service = game:GetService("ContextActionService")

function block(action, state, object)
	if (state == Enum.UserInputState.End) then
		-- Block
	end
end

context_action_service:BindAction("Block", block, false, Enum.KeyCode.Q)

If we didn't do this check, the player would block once when they pressed Q, and again when they released Q.

Now onto the animation. What you have is this:

anim.AnimationId = "rbxassetid://"

This is an empty Id. You need to take the Id of your blocking animation and stick it in front of this. It'll be something more like "rbxassetid://878925720"

Now we can play the animation:

local context_action_service = game:GetService("ContextActionService")
local block_anim = Instance.new("Animation")
block_anim.AnimationId = "rbxassetid://878925720" -- just a placeholder

function block(action, state, object)
	if (state == Enum.UserInputState.End) then
		local anim_track = humanoid:LoadAnimation(block_anim)
		anim_track:Play()
	end
end

context_action_service:BindAction("Block", block, false, Enum.KeyCode.Q)

A few extra things:

It's good that you're keeping an eye on the humanoid before trying to load it, but this isn't an ideal way of doing it:

repeat wait() until player.Character.Humanoid

You're putting yourself at risk of the Character not loading before you try and access it. This is better:

local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait() -- wait for the char to load first
local humanoid = character:WaitForChild("Humanoid") -- now wait for the humanoid

You also mentioned you wanted it to "zoom out a little when Q is pressed". To do this you can tweak the FieldOfView property of the player's camera:

local camera = workspace.CurrentCamera

camera.FieldOfView = 80 -- tweak between 20 and 80 to see what it does

Putting it all together:

local context_action_service = game:GetService("ContextActionService")

local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera

local block_anim = Instance.new("Animation")
block_anim.AnimationId = "rbxassetid://878925720" -- just a placeholder

function block(action, state, object)
	if (state == Enum.UserInputState.End) then
		local anim_track = humanoid:LoadAnimation(block_anim)
		anim_track:Play()

		camera.FieldOfView = 80 -- or whatever
	end
end

context_action_service:BindAction("Block", block, false, Enum.KeyCode.Q)

P.S.: I did this in 1 run, without looking back or proofreading. There will almost definitely be typos and things you might need to tweak a little, but the essentials are there.

Further reading: ContextActionService UserInputService Camera

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