How can I know how long has it been since an action happened in a script

So I am making a combat system and I want to have a timer that will start from 0 every time a player will punch, there are 4 punches/animations and I want to know if it has been more than 1.3 seconds since I clicked so I can reset the animation to start from the first one.

I just one to know a way of doing this.

you can use os.time() or os.clock() and get the elasped time (currentTime - timestamp)

local lastFire = os.clock(); -- keep track of the current time here

task.wait(2) -- wait a bit
local elaspedTime = os.clock() - lastFire -- elasped time since we first got the data

if elaspedTime > 1.3 then -- time check (if the time has goe over 1.3 seconds)
    -- do something
end

3 Likes

Thank you, I’ll try that right now

1 Like

You can use duration - tick() where you save the duration as = tick() then -tick() whenever you want to check how long has it been.

local duration = tick()
repeat
task.wait()
until tick() - duration >= 45

1 Like

It is working but there is a problem, If I click more than one time there will be 2 or more timers, some of them from the past that will reset my punch system, so how would I implement this to always have just one timer?

Here is the code:

coroutine.resume(coroutine.create(function()
	task.wait(2)
	local elapsedTime1 = os.clock() - timer
	
	if elapsedTime1 >= 1.3 then
		count = 1
		char:SetAttribute("Combo", 1)
	end
end))
2 Likes

You may use the tick() function instead to know how long it has been

So by using tick there won’t be more than one timer?

Add a delay, like a variable “coroutine” to prevent run/using more than 1 timer.

can you show me an example on how to do it?

Sure. Here is an example of the code:

local corountine = false
coroutine.resume(coroutine.create(function()
       if not corountine then
             corountine = true
	         task.wait(2)
	         local elapsedTime1 = os.clock() - timer
	
	        if elapsedTime1 >= 1.3 then
		      count = 1
		      char:SetAttribute("Combo", 1)
	        end
        corountine = false
     end
end))
2 Likes

The code doesn’t give me any errors but nothing happens, I even placed a print statement to see if it is working, I’ll try to see from what this thing might come from. If you have any ideas please tell me. Thank you for your time

By the way, did you declare the timer variable?

yes, it is tick() like you said

So rn it is working, the problem was that the timer was declared with tick and the check in the if-statement with os.clock

1 Like

But there is the same problem, there are multiple timers.

To be more precise, if I click 3 times, and then I stop, my combo will reset 3 times because there are 3 timers, but I want to reset just once when I stop

I think of doing an input ended function and place there this check but it would do the same thing, more timers

You don’t need the coroutine function here.

If I don’t have one, I would wait 2 seconds before being able to click again

cant you just make the timers separate variables and only reset that one specific timer