I am trying to make a script that if you press "q" and then hit "q" again it would get the time inbetween those two button presses. So for example. If I press "q" 1,2,3,4,5 I press q then it prints the time so it would be 5 seconds since the last time I pushed q was 5 seconds ago. If that makes sense anyway. I'm trying to make a beat type of game. But in order to do that I need to point out the beats manually so I had an idea to just have a table that has number value's in it.
tick
function.The tick function returns the amount of seconds since January 1st, 1970 You can use this to log two clicks. These two clicks will each be separate amount of seconds since the date stated before.. so what you can do is find the difference between the click logs - this way it's the difference in seconds!
UserInputService
, with the InputBegan
functionThe InputBegan function of UserInputService is a great replacement to KeyDown
, it returns any type of user input. So what you can do is compare the given input's KeyCode with a KeyCode Enum.. such as Enum.KeyCode.Q
--UserInputService local uis = game:GetService('UserInputService') --Last time clicked local lastTime = nil --InputBegan uis.InputBegan:connect(function(input) --Compare KeyCodes if input.KeyCode == Enum.KeyCode.Q then --If there is no lastTime if lastTime == nil then --Make a lastTime lastTime = tick() --Otherwise else --Get the difference in times local thisTime = tick() local difference = (thisTime - lastTime) --Print a rounded nearest number print(math.floor(difference + .5)) end end end)
Well, since you haven't shown that you've attempted this yourself, I'll just give you some pointers. I'm sure you know you have to use the KeyDown event, so what I'd personally do is set up a separate time script. It would be a while loop, and the script itself would be disabled. Then, I'd put two values. "ButtonCount" and "Timer". ButtonCount will be able to count how many button pushes of "q", so you can reset it after 2. The Hierarchy would be the following:
KeyDown script ButtonCount Timer TimingScript
So, in the TimingScript, it'd be the following:
while wait(1) do script.Parent.Timer.Value = script.Parent.Timer.Value + 1 end
Within the KeyDown script, have it so whenever it pushes it, there's an if statement. If the ButtonCount is already 1 (Before you add 1), then put it to 0 and disable the TimingScript, then do whatever you'd like with the Timer value. However, if it's 0, then add 1 to ButtonCount, and end the code.
Hope I helped!