Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Last active November 29, 2020 04:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howmanysmall/808eccbcd465ab4c74f44f72c8e0a2fa to your computer and use it in GitHub Desktop.
Save howmanysmall/808eccbcd465ab4c74f44f72c8e0a2fa to your computer and use it in GitHub Desktop.
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