local r=0 function Clock.new(f)local a=Instance.new("\078\117\109\098\101\114\086\097\108\117\101")a.Name=f a.Parent=ReplicatedStorage local m={initialTimePosition=r,initialClockTime=nil;timePosition=a,timeKeepingConnection=nil;pauseTime=nil}setmetatable(m,Clock)return m end function Clock.getCurrentTime(r)return os.clock()end function Clock._keepTrackOfTime(r)r:_stopTrackingTime()r.initialClockTime=r:getCurrentTime()r.timeKeepingConnection=r.RunService.Heartbeat:Connect(function()if r.pauseTime then r.timePosition.Value=r.pauseTime else local f=r:getCurrentTime()-r.initialClockTime r.timePosition.Value=f+r.initialTimePosition end end)return end function Clock.Pause(r)r.pauseTime=r.timePosition.Value r.pausedAtTime=r:getCurrentTime()end function Clock.Unpause(r)r.pauseTime=nil r.pausedAtTime=r.pausedAtTime or 0 r.initialClockTime=r.initialClockTime+(r:getCurrentTime()-r.pausedAtTime)end function Clock._stopTrackingTime(r)if r.timeKeepingConnection then r.timeKeepingConnection:Disconnect()r.timeKeepingConnection=nil end end function Clock.StartTime(r)(coroutine.wrap(function()r:_keepTrackOfTime()end))()end function Clock.ResetTime(f)f:_stopTrackingTime()f.timePosition.Value=r f.initialTimePosition=r end function Clock.SetTime(f,r)f:_stopTrackingTime()f.timePosition.Value=r f.initialTimePosition=r end return Clock

local startingTimePositionValue = 0

function Clock.new(timeId)
	local timePosition = Instance.new("NumberValue") -- Could this be slow, does that matter
	timePosition.Name = timeId
	timePosition.Parent = ReplicatedStorage

	local self = {
		initialTimePosition = startingTimePositionValue,
		initialClockTime = nil,
		timePosition = timePosition,
		timeKeepingConnection = nil,
		pauseTime = nil,
	}
	setmetatable(self, Clock)

	return self
end

function Clock:getCurrentTime()
	return os.clock()
end

function Clock:_keepTrackOfTime()
	self:_stopTrackingTime()
	self.initialClockTime = self:getCurrentTime()

	self.timeKeepingConnection = self.RunService.Heartbeat:Connect(function()
		if self.pauseTime then
			self.timePosition.Value = self.pauseTime
		else
			local differenceSinceCall = self:getCurrentTime() - self.initialClockTime
			self.timePosition.Value = differenceSinceCall + self.initialTimePosition
		end
	end)

	return
end

function Clock:Pause()
	self.pauseTime = self.timePosition.Value
	self.pausedAtTime = self:getCurrentTime()
end

function Clock:Unpause()
	self.pauseTime = nil
	self.pausedAtTime = self.pausedAtTime or 0
	self.initialClockTime = self.initialClockTime + (self:getCurrentTime() - self.pausedAtTime)
end

function Clock:_stopTrackingTime()
	if self.timeKeepingConnection then
		self.timeKeepingConnection:Disconnect()
		self.timeKeepingConnection = nil
	end
end

function Clock:StartTime()
	-- TODO: use luke's coroutine wrap because of silencable errors
	coroutine.wrap(function()
		self:_keepTrackOfTime()
	end)()
end

function Clock:ResetTime()
	self:_stopTrackingTime()

	self.timePosition.Value = startingTimePositionValue
	self.initialTimePosition = startingTimePositionValue
end

function Clock:SetTime(timePositionValue)
	self:_stopTrackingTime()

	self.timePosition.Value = timePositionValue
	self.initialTimePosition = timePositionValue
end

return Clock
