-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.luau
More file actions
73 lines (64 loc) · 1.63 KB
/
Copy pathinit.luau
File metadata and controls
73 lines (64 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
local Timer = {}
Timer.__index = Timer
export type Timer = {
new: (countTime: number, isCountDown: boolean) -> Timer,
elapsed: number,
Destroy: (Timer) -> (),
Start: (Timer) -> (),
StartTime: number,
TimeChanged: RBXScriptSignal,
TimerEnded: RBXScriptSignal,
GetElapsed: (Timer) -> number,
}
function Timer.new(countTime: number, isCountDown: boolean?)
local self = setmetatable({}, Timer)
self._timerEnded = Instance.new("BindableEvent")
self._timeChanged = Instance.new("BindableEvent")
self.TimerEnded = self._timerEnded.Event
self.TimeChanged = self._timeChanged.Event
self.COUNT_TIME = countTime
self.CountDown = if isCountDown ~= nil then isCountDown else true
self.elapsed = 0
return self
end
function Timer:GetElapsed()
return self.elapsed
end
function Timer:Start(startTime: number?)
if self._isRunning then
warn("timer already running")
return
end
self._isRunning = true
self.StartTime = startTime or os.time()
self.timerThread = task.spawn(function()
while self.COUNT_TIME > self.elapsed do
self:_changed(self.elapsed)
self.elapsed += 1
task.wait(1)
end
self._timerEnded:Fire()
self.timerThread = nil
end)
end
function Timer:Stop()
if self.timerThread then
task.cancel(self.timerThread)
end
end
function Timer:_changed(currentTime: number)
if self.CountDown == true then
self._timeChanged:Fire(self.COUNT_TIME - currentTime)
elseif self.CountDown == false then
self._timeChanged:Fire(currentTime)
end
end
function Timer:Destroy()
if self.timerThread ~= nil then
task.cancel(self.timerThread)
end
self._timerEnded:Destroy()
self._timeChanged:Destroy()
table.clear(self)
end
return Timer