Last active
November 29, 2020 04:14
-
-
Save howmanysmall/808eccbcd465ab4c74f44f72c8e0a2fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local HourDifference = math.floor((os.time() - tick()) / 900 + 0.5) * 900 | |
local BindableEvent = require(script.Parent.BindableEvent).new | |
local RandomPoller = { | |
ClassName = "RandomPoller"; | |
__index = { | |
Cancel = function(self) | |
self.Running = false | |
end; | |
Pause = function(self) | |
self.Paused = true | |
end; | |
Resume = function(self) | |
self.Paused = false | |
self.BindableEvent:Fire() | |
end; | |
}; | |
} | |
local function RandomPollerLoop(self, MinInterval, MaxInterval, Function) | |
while true do | |
if self.Paused then self.BindableEvent:Wait() end | |
local CurrentTime = tick() + HourDifference | |
local Interval = MinInterval + math.random() * (MaxInterval - MinInterval) | |
local TimeElapsed = CurrentTime + wait(Interval - CurrentTime % Interval) | |
if self.Running then | |
Function(TimeElapsed) | |
else | |
break | |
end | |
end | |
end | |
function RandomPoller.new(MinInterval, MaxInterval, Function) | |
math.randomseed(tick() % 1 * 1E7) | |
local self = setmetatable({ | |
Running = true; | |
Paused = false; | |
BindableEvent = BindableEvent(); | |
}, RandomPoller) | |
local Thread = coroutine.create(RandomPollerLoop) | |
coroutine.resume(Thread, self, MinInterval, MaxInterval, Function) | |
return self | |
end | |
return RandomPoller |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment