ITS_SELINASOON

Extreme precision wait

Jul 28th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. local RS = game:GetService("RunService")
  2. local EZLoop = {}
  3. EZLoop.__index = EZLoop
  4.  
  5. function EZLoop.new(time : number?)
  6.     time = time or 0
  7.     assert(typeof(time) == "number","Time given value expected to receive number")
  8.     assert(time >= 0,"Time expected to receive positive number")
  9.    
  10.     local self = setmetatable({},EZLoop)
  11.    
  12.     self.Time = time
  13.     self._connection = nil
  14.     self._bindable = Instance.new("BindableEvent")
  15.     self._bindableBeforeLoop = Instance.new("BindableEvent")
  16.     self._timing = 0
  17.     self._lastdelta = 0
  18.     self._paused = false
  19.    
  20.     self._connection = RS.Heartbeat:Connect(function(dt)
  21.         if self._paused == true then return end
  22.         self._lastdelta = dt
  23.         self._timing += dt
  24.         if self._timing >= self.Time - self._lastdelta then
  25.             self._bindableBeforeLoop:Fire(dt)
  26.         end
  27.         if self._timing >= self.Time then
  28.             self._bindable:Fire(dt)
  29.             self._timing = 0
  30.         end
  31.     end)
  32.  
  33.     return self
  34. end
  35.  
  36. function EZLoop:OneTimeLoop(callback : "function",... : any)
  37.     if typeof(callback) ~= "function" then
  38.         warn("Callback is not a function, got "..typeof(callback).." instead")
  39.         return
  40.     end
  41.    
  42.     local toSend = {...}
  43.    
  44.     coroutine.wrap(function()
  45.         self:Wait()
  46.         callback(table.unpack(toSend))
  47.     end)()
  48. end
  49.  
  50. function EZLoop.ScheduleFunction(callback : "function",duration : number,... : any)
  51.     if typeof(callback) ~= "function" then
  52.         warn("Callback is not a function, got "..typeof(callback).." instead")
  53.         return
  54.     end
  55.  
  56.     local toSend = {...}
  57.     local connection = nil
  58.     local timed = 0
  59.    
  60.     connection = RS.Heartbeat:Connect(function(dt)
  61.         if (timed + dt) >= duration then
  62.             connection:Disconnect()
  63.             connection = nil
  64.             callback(table.unpack(toSend))
  65.         else
  66.             timed += dt
  67.         end
  68.     end)
  69. end
  70.  
  71. function EZLoop:Pause()
  72.     self._paused = true
  73. end
  74.  
  75. function EZLoop:Resume()
  76.     self._paused = false
  77. end
  78.  
  79. function EZLoop:Reset()
  80.     self._timing = 0
  81. end
  82.  
  83. function EZLoop:Wait() : "YIELD"
  84.     self._bindable.Event:Wait()
  85. end
  86.  
  87. function EZLoop:AdjustTime(newTime : number)
  88.     self.Time = newTime or 0
  89. end
  90.  
  91. function EZLoop:GetTime() : number
  92.     return self.Time
  93. end
  94.  
  95. function EZLoop:Looped() : RBXScriptSignal
  96.     return self._bindable.Event
  97. end
  98.  
  99. function EZLoop:BeforeLoop() : RBXScriptSignal
  100.     return self._bindableBeforeLoop.Event
  101. end
  102.  
  103. function EZLoop:Destroy()
  104.     self._connection:Disconnect()
  105.     self._connection = nil
  106.     setmetatable(self,nil)
  107. end
  108.  
  109. return EZLoop
  110.  
Add Comment
Please, Sign In to add comment