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
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"
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()
local humanoid = character:WaitForChild("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
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"
function block(action, state, object)
if (state == Enum.UserInputState.End) then
local anim_track = humanoid:LoadAnimation(block_anim)
anim_track:Play()
camera.FieldOfView = 80
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
2 comments
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.
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.