Timer Module - Manage timers and their states at no cost!

Here’s another fix

local function RemoveFromArray(TimerObj)
     table.remove(TimerArrays, table.find(TimerArrays, TimerObj))
end

If :Stop() or :Pause() is accidentally called on a timer that was never started then this function will remove the last item from the TimerArrays table. Needs to look like this instead.

local function RemoveFromArray(TimerObj)
	local index = table.find(TimerArrays, TimerObj)
	if index then
		table.remove(TimerArrays, index)
	end
end
1 Like

@Dued1 Good catches, especially for the latter ones.
I’m unsure regarding the binary search algorithm since it was provided to be by CntKillMe, and after looking through other sources they use math.floor too. I’ll do some extra research before properly taking care of the issue.

[Update 1.0.6]

  • Validates that the timer object has been started before removing it from the priority queue.

  • Fixed issues with binary search algorithm; the issue lied in the fact that since the binary search algorithm was translated from roblox-ts in which arrays start at 0 rather than 1, I forgot to add a + 1 within the code where I grab the middle timer. I have rewritten the algorithm function to function on a 1-based index rather than a 0-based index (thank you CntKillMe again).

2 Likes

I have the same issue as @Sweet_Smoothies. Am I doing something wrong? Here is my code

local timer = Timer.new(5)

print("starting timer")

timer:Start()

timer:Wait()

print("timer ended")

“timer ended” never prints

1 Like

When updating the module previously I didn’t properly make sure yielding worked. This issue should be addressed now.

1 Like

100% will use this module in my game!
Timers are pretty cool since i never could make one lol

1 Like

Would this timer be useful for Hourly/Daily rewards? as a 2 hour timer as an example

Do the timer objects created garbage collect themselves or do they just get removed when the timer ends? I don’t see a method to destroy or otherwise set the timer object to nil in the documentation, is that by deign?

How can we be sure they are cleaned up and not causing memory leaks?

1 Like

I noticed that the callbacks for the states “Died” and “Dead” do not fire. This may be intended, but if so I am not sure how to fire a function if it is Stopped. We can fire a function if it is Finished yes, but what if we kill it before its finished.

Update [1.0.7]

  • Put the whole source and documentation on my GitHub repository. I will no longer be updating the uploaded Roblox asset.
  • Addressed issue where OnDied | OnDead would not fire. cc @Planet_Dad
2 Likes

Sorry, I’m not well-versed in this area of expertise, and no one I knew was available to help me determine whether this would cause any memory leaks. I’ve been using it fine for a while without any noticeable memory increases, though.

1 Like

There doesn’t appear to be anything in his code that would cause a memory leak, it looks like if you want to get rid of a timer object that you should cancel it’s timer if it is active and set any references you have to it to nil and it will garbage collect.

1 Like

how would i restart the time because ive tried timer:stop() timer:start()

1 Like

Is it possible to change the timer mid time? like I have 50 seconds and be able to change it to 6 or something like that

Could you use this similar to a stop watch, or is it only for waiting 5 seconds for example?