Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local RS = game:GetService("RunService")
- local EZLoop = {}
- EZLoop.__index = EZLoop
- function EZLoop.new(time : number?)
- time = time or 0
- assert(typeof(time) == "number","Time given value expected to receive number")
- assert(time >= 0,"Time expected to receive positive number")
- local self = setmetatable({},EZLoop)
- self.Time = time
- self._connection = nil
- self._bindable = Instance.new("BindableEvent")
- self._bindableBeforeLoop = Instance.new("BindableEvent")
- self._timing = 0
- self._lastdelta = 0
- self._paused = false
- self._connection = RS.Heartbeat:Connect(function(dt)
- if self._paused == true then return end
- self._lastdelta = dt
- self._timing += dt
- if self._timing >= self.Time - self._lastdelta then
- self._bindableBeforeLoop:Fire(dt)
- end
- if self._timing >= self.Time then
- self._bindable:Fire(dt)
- self._timing = 0
- end
- end)
- return self
- end
- function EZLoop:OneTimeLoop(callback : "function",... : any)
- if typeof(callback) ~= "function" then
- warn("Callback is not a function, got "..typeof(callback).." instead")
- return
- end
- local toSend = {...}
- coroutine.wrap(function()
- self:Wait()
- callback(table.unpack(toSend))
- end)()
- end
- function EZLoop.ScheduleFunction(callback : "function",duration : number,... : any)
- if typeof(callback) ~= "function" then
- warn("Callback is not a function, got "..typeof(callback).." instead")
- return
- end
- local toSend = {...}
- local connection = nil
- local timed = 0
- connection = RS.Heartbeat:Connect(function(dt)
- if (timed + dt) >= duration then
- connection:Disconnect()
- connection = nil
- callback(table.unpack(toSend))
- else
- timed += dt
- end
- end)
- end
- function EZLoop:Pause()
- self._paused = true
- end
- function EZLoop:Resume()
- self._paused = false
- end
- function EZLoop:Reset()
- self._timing = 0
- end
- function EZLoop:Wait() : "YIELD"
- self._bindable.Event:Wait()
- end
- function EZLoop:AdjustTime(newTime : number)
- self.Time = newTime or 0
- end
- function EZLoop:GetTime() : number
- return self.Time
- end
- function EZLoop:Looped() : RBXScriptSignal
- return self._bindable.Event
- end
- function EZLoop:BeforeLoop() : RBXScriptSignal
- return self._bindableBeforeLoop.Event
- end
- function EZLoop:Destroy()
- self._connection:Disconnect()
- self._connection = nil
- setmetatable(self,nil)
- end
- return EZLoop
Add Comment
Please, Sign In to add comment