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

How can a make a intValue increase when key is pressed?

here is what i have so far

local M = game.Players.LocalPlayer:GetMouse()
HI = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton:WaitForChild("intiger")
Animation = script:WaitForChild("Animation")
local player = game.Players.LocalPlayer
hp = player.Character.Humanoid
M.KeyDown:Connect(function(key)
	if key == "f" then
		local Animationtrack = hp:LoadAnimation(Animation)
		Animationtrack:Play()
		HI.Value = HI.Value + 1
	end
end)

4 comments

0

omfg not this crap again

0

use the UserInputService or ContextActionService

0

KeyDown is a keyboard event, not a mouse event

0

KeyDown is an event of mouse. The only issue is that it is deprecated.

1 answer

#UserInputService UserInputService is much better than KeyDown (since UserInputService can get inputs from game controllers and touchscreen).

--Must be done in a local script
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 --Make sure it is a keyboard input
   		if key.KeyCode == Enum.KeyCode.F then
   	    	local humanoid = player.Character:FindFirstChild("Humanoid")
   	    	if humanoid then --If humanoid exists
   	        	local playAnim = humanoid:LoadAnimation(Animation)
   	        	playAnim.Priority = Enum.AnimationPriority.Action --Highest priority animation
   	        	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!

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