Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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)

0
omfg not this crap again DeceptiveCaster 3761 — 5y
0
use the UserInputService or ContextActionService DeceptiveCaster 3761 — 5y
0
KeyDown is a keyboard event, not a mouse event DeceptiveCaster 3761 — 5y
0
KeyDown is an event of mouse. The only issue is that it is deprecated. EzraNehemiah_TF2 3552 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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!

Ad

Answer this question