I’ve been needing a timer module, and found this thread of a feature request for such a thing to be implemented properly:
Since I couldn’t find anyone posting anything else about it, I wanted to try to make a neat module for it myself. This has changed over time - initially it was quite bad, but it’s improved to the point I’d be happy for people to use it (also, it works completely as I’d expect a timer module to now, finally).
Documentation
Methods:
Events:
Properties:
Simple example use case:
Changelog
19-May-2021:
Added infinite duration condition (Duration can be 0 or less to be infinite)
Added pause function with :Pause(), which can be unpaused using :Start()
Added Timer.TimeElapsed (shows how long it has been since the timer started. Paused timespan not taken into account)
Further polishing, should function cleanly and as expected now
Changed some variables.
Changed :Stop() method to :Destroy() (more consistent with Roblox instances)
Nice release, one thing I would say to implement is a pause function. Might be helpful to those that want to be able to pause the time due to a variable able to pause, and then resume after variable is removed.
Was going to add a pause function initially but decided not to so I could work on other things for now. I’ll probably add an Extend function too, but I don’t want to start bloating the API too much.
Could you send me the script so I could see how it works? I’m still fairly new to OOP, so I’d love to learn how yours is faster. It’s fine if you want to keep it private, though.
I’m not sure what he used for his event, but here’s something rudimentary I made awhile ago that I didnt end up using much.
Not sure what you could want it for, but it’s fired by calling the event object.
I believe I used it for a trajectory system I made, had an impact method.
Anyways, I’d recommend rewriting the arguments part.
Edit:
This was also meant to be used with other object oriented stuff. For example
Small update; added a Count event to this module. Useful for when you want an event to fire every x seconds. Right now it just returns the real elapsed time since the start (in that event) but I’ll make it return the ideal elapsed time (ignoring the annoying decimal numbers tick() produces).
Works for me - make sure you’re using it right. If you want it to count at a different step, you should change the CountStep property. There might be a better way to do this, and to be honest, this module is quite old at this point. Should still function well though - let me know if there are any bugs.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Timer = require(ReplicatedStorage.ModuleScripts.Timer)
local module = {}
local ComboTimer = Timer.new(2.5)
function module.Function (player)
local combo = player.PvPStats:FindFirstChild("Combo")
local combocounter = player.PlayerGui.ComboCounter
local textlabel = combocounter.Frame.TextLabel
ComboTimer.Count:Connect(function(elapsedTime)
print(elapsedTime)
if elapsedTime < 2.5 then
ComboTimer:Stop()
ComboTimer:Start()
print("Restarted timer")
elseif elapsedTime == 2.5 then
print("started timer")
ComboTimer:Start()
end
end)
ComboTimer.Completed:Connect(function()
print("timer Ended")
end)
end
return module
the count does work but i have to start it first thats a problem for me. so whenever u hit someone a combo value is increased by 1 . i want to add a timer and when the timer runs out the combo will be set back to 0 but how would i reset da timer
This timer module was a bit scuffed. The timer started when you created it, instead of when the Start() method was called. That has been amended now, and I’ve just added a Reset() method.
Should be neater now. Apologies for those of you who had issues with this.
@Roastdbacon you should reinsert the module, and then you’ll be able to use the new Reset function. Make sure you account for the timer not ‘starting’ immediately now (it started counting but wouldn’t fire events until :Start() was called) though, in case you were relying on this behaviour for something before.
I also use timers that can be cancelled most of the time but I just use the tweenservice :cancel() and :Play() as alternative since the .Completed gives a playbackstate of whether it is cancelled or completed.
Update! I fixed a load of issues with this, and finally made it work smoothly as I intended it. I don’t expect to update this unless there are any other issues or optimisations I should make to it (though, I don’t expect it to be perfectly optimised right now but it’s definitely useful).
Added more functionality such as allowing an infinite duration easier, pausing, checking the elapsed time, and also changing the documentation to show all of the properties/variables the timer has. This should be a lot more usable now, and I apologise for any issues trying to use it previously. Let me know if this is helpful, I appreciate any feedback.
I am using this in Slither.io Simulator as a replacement for delay() that I can also cancel as needed. It is a very useful tool.
I wanted to share with the community, my remix which is in TypeScript. I am using this with roblox-ts to transpile it to Lua. The gist also includes an example usage, taken from my MapStateTracker class that I use in my game to control transitions from one Map to the next.