#UserInputService
UserInputService is much better than KeyDown (since UserInputService can get inputs from game controllers and touchscreen).
game:GetService("UserInputService").InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.F then
print("F was pressed")
end
end)
Use this site if you want to see the Enum index for keycodes.
#Animation Priority
The reason why the animation may not be running is because Animations have different priorities dictating which animation be played over another. Here is a list of enums for AnimationPriority. If you want an animation to be played, you should set its priority to something higher than the default animations (action is the highest priority). If an animation is played and another animation is played with the same priority, the first animation is stopped over the second one.
#Final Product
Also, make sure you identify the variable hp inside of the event, not outside. In case the character respawns, it'll use the new humanoid.
local HI = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton:WaitForChild("intiger")
local Animation = script:WaitForChild("Animation")
local player = game:GetService("Players").LocalPlayer
game:GetService("UserInputService").InputBegan:Connect(function(key)
if key.UserInputType == Enum.UserInputType.Keyboard then
if key.KeyCode == Enum.KeyCode.F then
local humanoid = player.Character:FindFirstChild("Humanoid")
if humanoid then
local playAnim = humanoid:LoadAnimation(Animation)
playAnim.Priority = Enum.AnimationPriority.Action
playAnim:Play()
HI.Value = HI.Value + 1
end
end
end
end)
Change Enum...Action to Enum...Core if you want animations like walking, falling, jumping or swimming to override it.
Hope it helps!
4 comments
omfg not this crap again
use the UserInputService or ContextActionService
KeyDown is a keyboard event, not a mouse event
KeyDown is an event of mouse. The only issue is that it is deprecated.