Hold e for 5 seconds

I am trying to do so you hold down e for 5 seconds and then a part gets removed. The closest i have gotten is so you click e 5 times for it to work. I also want it to reset when you let go of the button. This is my code so far:

local UIS = game:GetService(“UserInputService”)

local Count = 0

UIS.InputBegan:connect(function(input,gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
if UIS:GetFocusedTextBox() == nil then
for i,v in pairs(game.Workspace.Scrap:GetChildren()) do
local mag = (v.Position-
game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
if mag <= 10 then
if Count >= 5 then
the scrap breaks.
v:Destroy()
else
Count = Count +1
end
end
end
end
else Count = 0
end
end
end)

2 Likes

You can use a while loop, which checks that :IsKeyDown for E is true:

local Count = 0

while UIS:IsKeyDown(Enum.KeyCode.E) do
    if Count >= 5 then
        Count = 0
        v:Destroy()
        break --Ends the while loop
    else
        Count = Count + 1
    end
    wait(1)
end

I’d recommend that you used some indentation for your Script & that you used the script formatting method:
```
–code
```
This can help others read your code and assist you further.

6 Likes

Use tick() to record the initial time when E was hold. Then use tick() + 5 to determine the goal before tick() has passed 5 seconds, until the time passed or when E was released early.

4 Likes