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

How do i make it so that the player has to hold down e for 5 seconds before my effect is fired?

Asked by 5 years ago
local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

local UIS = game:GetService("UserInputService")

local Part = game.Workspace.GUIPart



UIS.InputBegan:connect(function(KeyCode)

if KeyCode.KeyCode == Enum.KeyCode.E then

if (Part.Position - HumanoidRootPart.Position).magnitude < 20 then

-- effect

print("HI")

end

end

end)
0
You could have checks on an interval for 5 seconds to see if the key is still held down, this could be represented by a variable that changes on Began and Ended, but feel free to provide an attempt of what you were trying to do since this code doesn't show an attempt related to the question. gullet 471 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Try this:

local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")

local UIS = game:GetService("UserInputService")

local Part = game.Workspace.GUIPart
local HoldCount = 0


UIS.InputBegan:connect(function(KeyCode)

if KeyCode.KeyCode == Enum.KeyCode.E then

if (Part.Position - HumanoidRootPart.Position).magnitude < 20 then
    while UIS:IsKeyDown(Enum.KeyCode.E) do
        wait(1)
        HoldCount = HoldCount + 1
        If HoldCount == 5 then
            break -- this will automatically end after 5 seconds
        end
    end
    HoldCount = 0 -- Reset hold count
    -- effect

    print("HI")

    end

end

Hope this helped! Best of luck developer.

BlackOrange3343

PS: If it doesn't work then comment the error

0
I also suggest a Debounce to avoid spam BlackOrange3343 2676 — 5y
0
Thank you very much BlackOrange3343! Ive been trying to figure out this problem for a while now and finally theres a solution! Narrowlegobricks 12 — 5y
0
No Problem just scanning through unanswered questions BlackOrange3343 2676 — 5y
Ad

Answer this question