Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Time Inbetween button was pressed?

Asked by
rootx 15
10 years ago

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.

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

What You Need

  • You need to take advantage of the 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!

  • You need to use UserInputService, with the InputBegan function

The 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


Code

01--UserInputService
02local uis = game:GetService('UserInputService')
03--Last time clicked
04local lastTime = nil
05 
06--InputBegan
07uis.InputBegan:connect(function(input)
08    --Compare KeyCodes
09    if input.KeyCode == Enum.KeyCode.Q then
10        --If there is no lastTime
11        if lastTime == nil then
12            --Make a lastTime
13            lastTime = tick()
14        --Otherwise
15        else
View all 23 lines...
0
Thank you! rootx 15 — 10y
Ad
Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

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:

1KeyDown script
2    ButtonCount
3    Timer
4    TimingScript

So, in the TimingScript, it'd be the following:

1while wait(1) do
2    script.Parent.Timer.Value = script.Parent.Timer.Value + 1
3end

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!

Answer this question