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

Problem With Animation During 'KeyDown' Function, How Do I Resolve This Problem?

When the player move's if I press the key to activate the animation it doesn't apply, how do i fix this problem?

local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService('UserInputService')
local AnimationId = 'rbxassetid://0'
local Key_Charge = 'Q'
local Toggle = true

  

Mouse.KeyDown:connect(function(key)
key = string.lower(key)
if string.byte(key) == 113 then
Toggle = true
local keyConnection = Mouse.KeyUp:connect(function(key)
if string.byte(key) == 113 then
Toggle = false
end
end)
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationId
local LoadAnimation = Humanoid:LoadAnimation(Animation)
LoadAnimation:Play()
repeat wait () until Toggle == false
keyConnection:disconnect()
Animation:Destroy()
end
end)

1 comment

0

Use InputBegan instead of KeyDown, and indent your code

1 answer

✓ Accepted answer

KeyDown is an old deprecated event, which means it will no longer work. (Deprecated means outdated and no longer usable). So instead you'd have to use the UserInputService and exactly InputBegan which is an event for that service. This event will fire whenever an input is detected, so pretty much when a keyboard key or mouse is clicked, and those are inputs. And for all the information you want here.

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
    --input paramater is the input that just got detected, the second paramater is a bit complicated, so check that link for info.
    if input.UserInputType = Enum.UserInputType.Keyboard then
    --this if statlent for example is to check if the input was a mouse, but youre never gonna need this.
	    print(input.KeyCode)
	    --now that will print the key that was clicked, .KeyCode represents the keycode of the input.
	end
end)

And that's all about it.

local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService('UserInputService')
local AnimationId = 'rbxassetid://0'
local Key_Charge = 'Q'
local Toggle = true

  
game:GetService("UserInputService").InputBegan:Connect(function(input)
key = input.KeyCode
if key == Enum.KeyCode.Q then
--this will check if the key was Q, we Enums a lot with this, thats the enum for all keycodes, and here for Q
Toggle = true
local keyConnection = game:GetService("UserInputService").InputBegan:Connect(function(input)
if key == Enum.KeyCode.Q then
Toggle = false
end
end)
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationId
local LoadAnimation = Humanoid:LoadAnimation(Animation)
LoadAnimation:Play()
repeat wait () until Toggle == false
keyConnection:disconnect()
Animation:Destroy()
end
end)

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