How can I understand and use tick() for time difference?

Hey,

how can I make a time difference with tick()?

Like when I pressed no input longer then 20minits then it does print("Hey)

– I just want do that with tick()

by the way I am new in tick() :smiley:

tick() is basically the time in seconds since 1st January 1970. A way I know how you could do is

local currentTick = tick()
local wantedTick = currentTick + (20 * 60)
repeat
     currentTick = tick()
     wait(1)
until currentTick >= wantedTick

print("Hey")

I’m not sure if this is the most optimal way to do it, but it is a start. What it does is stores the current tick in currentTick and makes a variable for the wanted tick, which is 20 minutes * 60 to get the amount of seconds. Then every second it repeatedly makes currentTick the tick it is right now until it is greater or equal to the tick we wanted, then it will print Hey

If I understood what you meant correctly, then here’s an example. It will only print after it has been 5 seconds or over.

local uis = game:GetService('UserInputService')
local old_tick = tick()
local delay_time = 5

uis.InputBegan:Connect(function(key, processing)
    if not processing and key.KeyCode == Enum.KeyCode.M then
        if tick() - old_tick >= delay_time then
            print('It has been 5 seconds or over.')
            old_tick = tick()
        end
    end
end)

You can adjust the time by changing the delay_time variable.

Also this code could be improved by using ContextActionService for specific binds, and you can also use os.clock() instead of tick().

2 Likes

oh wait your script doesnt seem work

It works fine for me, are you running it in a local script?

Oh yes I am sorry it does work