Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

1 comment

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.

1 answer

✓ Accepted answer

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

3 comments

0

I also suggest a Debounce to avoid spam

0

Thank you very much BlackOrange3343! Ive been trying to figure out this problem for a while now and finally theres a solution!

0

No Problem just scanning through unanswered questions

This discussion is preserved as it was. New answers, comments, and votes are closed.