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 you have to told the Key for a certain period of time?

I'm trying to make it so the user has to hold the key down 3 seconds but i end up breaking the script everytime i try to

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Replicated = script.Parent
local event = Replicated:WaitForChild("Use")
local deactivate = Replicated:WaitForChild("Not")
local using = false
local cooldown = false
mouse = Player:GetMouse()

holding = false

mouse.KeyDown:connect(function(key)
if key == 'm' then
	if holding == false then

holding = true
event:FireServer()
	  else
		holding = false
		deactivate:FireServer()
end
end

end)









2 answers

✓ Accepted answer

Hello, BookOfTragedy!

The script below should help you out. This script is just like yours but I've changed some stuff. Everything that is changed has been explained in the script.

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)

I would recommend using UIS over KeyDown as KeyDown is deprecated. UIS Docs. For this problem, I would use the InputBegan event and IsKeyDown function. You would use InputBegan to change the holding variable to true if the key m is pressed and you would use IsKeyDown to see if m has ever been let go before the 3 seconds is up. (You can find all of this in the UIS Docs). Reply to this if you have more questions.

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