-- Compiled with roblox-ts v3.0.0 local TS = _G[script] local RunService = TS.import(script, TS.getModule(script, "@rbxts", "services")).RunService local Timer local Builder do Builder = setmetatable({}, { __tostring = function() return "Builder" end, }) Builder.__index = Builder function Builder.new(...) local self = setmetatable({}, Builder) return self:constructor(...) or self end function Builder:constructor() self.timer_ = Timer.new() self.auto_start_ = false end function Builder:Create() return Timer.Builder.new() end function Builder:WithTimer(callback) callback(self.timer_) return self end function Builder:WithWaitTime(value) self.timer_.WaitTime = value return self end function Builder:WithOneShot(value) self.timer_.OneShot = value return self end function Builder:WithTimeOutCallback(callback, with_connection) local connection connection = self.timer_.OnTimeOut:Connect(function() return callback(connection) end) local _result = with_connection if _result ~= nil then _result(connection) end return self end function Builder:WithAutoStart() self.auto_start_ = true return self end function Builder:Build() if self.auto_start_ then self.timer_:Start() end return self.timer_ end end setmetatable(Builder, { __call = function() return Builder.new() end, }) do Timer = setmetatable({}, { __tostring = function() return "Timer" end, }) Timer.__index = Timer function Timer.new(...) local self = setmetatable({}, Timer) return self:constructor(...) or self end function Timer:constructor() self.WaitTime = 1 self.time_left_ = 0 self.OneShot = true self.Paused = false self.stopped_ = true self.time_out_event_ = Instance.new("BindableEvent") self.OnTimeOut = self.time_out_event_.Event end function Timer:GetTimeLeft() return self.time_left_ end function Timer:IsStopped() return self.stopped_ end function Timer:Start(time_sec) if time_sec == nil then time_sec = -1 end if time_sec > 0 then self.WaitTime = time_sec end --stops the existing timer self:Stop() self.time_left_ = self.WaitTime --takes stopped flag away self.stopped_ = false self.update_connection_ = RunService.Heartbeat:Connect(function(delta_time) return self:Update(delta_time) end) end function Timer:Update(delta_time) --dont update if paused if self.Paused then return nil end self.time_left_ -= delta_time if self.time_left_ > 0 then return nil end self.time_out_event_:Fire() if not self.OneShot then --restarts the time if not oneshot self.time_left_ = self.WaitTime return nil end --stops if one shot self:Stop() end function Timer:Stop() if self.stopped_ then return nil end --clamps the time self.time_left_ = math.max(self.time_left_, 0) local _result = self.update_connection_ if _result ~= nil then _result:Disconnect() end self.Paused = false self.stopped_ = true end function Timer:Destroy() self:Stop() self.time_out_event_:Destroy() end Timer.Builder = Builder end return { Timer = Timer, }