-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSyncedPoller.lua
50 lines (38 loc) · 1.25 KB
/
SyncedPoller.lua
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
-- Polling synced to os.time()
-- @author Validark
local Resources = require(game:GetService("ReplicatedStorage"):WaitForChild("Resources"))
local Table = Resources:LoadLibrary("Table")
local tick, wait = tick, wait
local HourDifference do
-- Get the difference in seconds between os.time() and tick(), rounded to the nearest 15 minutes
local SecondsPer15Minutes = 60 * 15
HourDifference = math.floor((os.time() - tick()) / SecondsPer15Minutes + 0.5) * SecondsPer15Minutes
end
local SyncedPoller = {}
SyncedPoller.__index = {}
function SyncedPoller.new(Interval, Func)
-- Calls Func every Interval seconds
-- @param number Interval How often in seconds Func() should be called
-- Obviously this uses `wait`, so 0 is a valid interval but it will in reality be about (1 / 30)
-- @param function Func the function to call
local self = setmetatable({
Running = true
}, SyncedPoller)
spawn(function()
while true do
local t = tick() + HourDifference
local TimeElapsed = t + wait(Interval - t % Interval)
if self.Running then
Func(TimeElapsed)
else
break
end
end
end)
return self
end
--- Cancels the previously instantiated poller.
function SyncedPoller.__index:Cancel()
self.Running = false
end
return Table.Lock(SyncedPoller)