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

How Would You Get the Time a Key is Held Down?

So I already have functions made for when a key is pressed, but I am unsure of how to get the number of seconds a key is held down. In other words, I need something to happen only after holding a key down for a certain number of seconds. Any help would be appreciated. Thanks in advanced.

1 answer

✓ Accepted answer

We need to save the time when the key is pressed down and compare it to when it is lifted.

im using the function time() which gives us the number of second after the server started

local lastTime=0

keydown.Connect(function()
	lastTime=time()
end)
keyup.Connect(function()
	local dif=time()-lastTime
end)

Edit

local lastTime=0
local down=false

keydown.Connect(function()
	lastTime=time()
	down=true
	while down do
		print(time()-lastTime)
		wait(1)
	end
end)
keyup.Connect(function()
	down=false
	local dif=time()-lastTime
end)

3 comments

0

How would you go about printing something to output for each second it is held down?

0

print(dif)

0

@corncob567 updated answer

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