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

How do i make it so you have to hold a key down for a period of time?

Even with assistance it still seems to not work what am i doing wrong?

local PlayerService = game:GetService("Players")
local UserInputService = game:GetService("UserInputService") -- Using UserInputService instead of mouse.

local Client = PlayerService.LocalPlayer

local Key = "M" -- The ke the person has to hold

local Holding = false -- Detects when the key is being held
local ExpectedHold = 3 -- How long you want the player to be hold the key for
local HoldingTime = 0 -- The amount of seconds they holded for

local Event = game.ReplicatedStorage:WaitForChild("Use")

UserInputService.InputBegan:Connect(function(Input, GPE)
    if not GPE then
        if Input.KeyCode == Enum.KeyCode[Key] then -- When they start to press the key this runs
            if not Holding then -- if they arent already holding then it continues
                Holding = true -- Makes holding true/script now knows they're holding the key

                while Holding do wait(1) -- Basically while holding == true the code after this will run every second
                    if HoldingTime < ExpectedHold then
                        HoldingTime = HoldingTime + 1 -- This adds to how long they holder the key for
                    end
                end
            end
        end
    end
end)


UserInputService.InputEnded:Connect(function(Input, GPE) -- runs when they stop holding a key
    if not GPE then
        if Input.KeyCode == Enum.KeyCode[Key] then
            if HoldingTime >= ExpectedHold then -- if they holding the key for the right amount of time
                Event:FireServer() -- fires the remote if they did
            else
                print(Client.Name..' holded '..Key..' for '..HoldingTime..' second(s).')
                HoldingTime = 0 -- sets the time back to 0
            end
        end
    end
end)

5 comments

0

Do you want the event to be able to fire while they're still holding the key down if it's over ExpectedHold, or only once they release the key?

0

Yeah while they are still holding down the key

0

@vector3_zero The script works like this. While they're holding down the key it detects how long they've held it for. Once they release, if it's greater than ExpectedHold it fires the remote

0

I Fixed this issue thanks!

0

I got the script working but now i can't deactivate it after once fired

1 answer

I would do a timed key press using this type of logic:

local RunService = game:GetService("RunService")

local Event = game.ReplicatedStorage:WaitForChild("Use")

local Key = "M"

local Holding = false
local HoldingTime = 0
local ExpectedHold = 3

-- Variable to record when press begins
local pressBegan

RunService.Heartbeat:Connect(function(step)
	if Holding then
		-- Increment HoldingTime while button is held down
		HoldingTime = HoldingTime + tick() - pressBegan
		
		-- Check if button has been held long enough
		if HoldingTime >= ExpectedHold then
			-- Fire event
			Event:FireServer()
			
			-- Set Holding to false so that we don't spam the event
			Holding = false
		end
	end
end)

UserInputService.InputBegan:Connect(function(Input, GPE)
	if not GPE and Input.KeyCode == Enum.KeyCode[Key] then
		Holding = true
		pressBegan = tick()
	end
end)

UserInputService.InputEnded:Connect(function(Input, GPE)
	if not GPE and Input.KeyCode == Enum.KeyCode[Key] then
		Holding = false
		HoldingTime = 0
	end
end)

I haven't tested it but something like that should do what you need I think.

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