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

Detect a held Key down?

Asked by 4 years ago
Edited 4 years ago

I m trying to make an action when a specific key is hold down and when the key is released the action stops. Everything work fine, The problem is when I test it and another key than the one specify is released the action stop. Like when the Key is hold down the action play but when you move and then stop moving the action stop while the specified key is always pressed down.

Can someone help me I cant figure it out, here is the script

local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local CoolDown = 5
local AnimationId = 'rbxassetid://2578167315'
local Debounce = true
local Key = 'Q'

game:GetService('UserInputService').InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
    if Input.KeyCode == Enum.KeyCode[Key] and Debounce == true then
        Debounce = false
        local Animation = Instance.new("Animation")
        Animation.AnimationId = AnimationId
        local LoadAnimation = Humanoid:LoadAnimation(Animation)
        LoadAnimation:Play()
        game.ReplicatedStorage.Events.Barrier:FireServer(CFrame.new(0,6,-5))


game:GetService("UserInputService").InputEnded:connect(function(input,IsTyping)
        if IsTyping then return end
    if Input.KeyCode == Enum.KeyCode[Key] and Debounce == false then
    game.ReplicatedStorage.Events.BarrierEnd:FireServer()
Debounce = true
end
end)
        end
end)

1 answer

Log in to vote
0
Answered by 4 years ago
local HoldingDownKey = false
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(Input,GameProcess)
    if not GameProcess then
        if Input.KeyCode == Enum.KeyCode.Q then
            HoldingDownKey = true
            -- do stuff here
        end
    end
end)
UserInputService.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.Q then
        HoldingDownKey = false
        -- do stuff here
    end
end)
Ad

Answer this question