UserInputService.InputBegan and UserInputService:IsKeyDown

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function()

   while UIS:IsKeyDown(Enum.KeyCode.E) do 
    		print("e is being held")
    	wait()
    	end
    end)

I was wondering if there is another way to detect is a key is being held down. I’m asking this because if you hold down ‘e’ and press another key, it will still be counted as ‘e’ being held down.
I’m asking this because I am trying to get a part to move while pressing keys by using CFrame’s in a script.

I’m sure there is probably an easy solution, however, I just cant think of one.

2 Likes

You could use the same kind of system but going through a different way.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
    if key == "e" then
        print("Pressed e")
    end
end)

So it uses the mouse.KeyDown feature.
I do hope this helps you in your scripting!

-uD0ge :dog:

2 Likes

I’ve tried this, the only problem is that it doesn’t detect whether ‘e’ is being held down or not, it only detects if it’s being pressed. Thank you for trying to help me anyway! :slight_smile:

Hmm, you could try this in a LocalScript.

local UIS = game:GetService("UserInputService")
local holdingUKey = false

UIS.InputBegan:Connect(function(inputObject) 
    if(inputObject.KeyCode==Enum.KeyCode.U)then
        holdingUKey = true

        while holdingUKey do
            print("Holding u key!")
            wait(0.2)
        end

    end
end)

UIS.InputEnded:Connect(function(inputObject)
    if(inputObject.KeyCode==Enum.KeyCode.U)then
        holdingUKey = false
    end
end)

This way it has a variable for when the player is holding it down. In this case the player is holding down ‘U’.

4 Likes