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
9 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
9 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

--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)
0
Thank you! rootx 15 — 9y
Ad
Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 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:

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!

Answer this question