Hold E to interact

wait(3)
print("hi")
if(UserInputService:IsKeyDown(Enum.KeyCode.LeftShift)) then
print("hi 2")
interaction:Destroy()
end

This is my way of doing Hold E to interact, however UserInputService:IsKeyDown is returning null. What is another way I could do this?

2 Likes

You are not getting the UserInputService. One way to do this is using GetService.

local UserInputService = game:GetService(“UserInputService”)

That isn’t my full code all variables are defined.

IsKeyDown() does not wait for inputs it checks at the time it is run

is there a way to detect if it being held

Check out this article for help. https://developer.roblox.com/en-us/api-reference/class/InputObject

You should use

UserInputService.InputBegan:Connect(function(k)
if k.keycode = enum.keycode.e then
print(“hi 2”)
end
end)

Runs on every keyboard input t

That isn’t the problem, the problem is to find out if I am HOLDING the key down.

on my gun script i use the following way to detect if a key is pressed down or not

BlaBla User Pressed E function(e)
Down = true
do this and that

BlaBla User Let go of E function(e)
Down = false

i just created a variable to detect if a player hold it down and then i made a loop and the loop gets broken
once down is false

local hold = 0
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, keypressed)
if input.KeyCode == Enum.KeyCode.E then
hold = hold + 1
-- if hold == 5(seconds has passed) then
-- continue code
button = input.InputEnded or InputChanged:Connect(function(inp)
if inp == "UserInputState" then
button:Disconnect()
end)

Something like this? Dunno if it’ll work but the premise is every time the counter reaches a set number, the interact menu will pop up.

1 Like

This video might help you.

If you’re trying to see if E is held, why are you checking left shift?
And if you wait it to be like for 3 seconds as I see the wait there, I usually do something like this:

local waiting = 0
while UserInputService:IsKeyDown(Enum.KeyCode.E) do
      wait(1)
      waiting = waiting + 1
      if(waiting == 3) then
            break
      end
      
end
if(waiting == 3) then
    --Code
else 
   waiting = 0
end

And that of course in the function.

3 Likes