local J=0 function Clock.new(s)local q=Instance.new("\078\117\109\098\101\114\086\097\108\117\101")q.Name=s q.Parent=ReplicatedStorage local d={initialTimePosition=J;initialClockTime=nil,timePosition=q,timeKeepingConnection=nil}setmetatable(d,Clock)return d end function Clock.getCurrentTime(J)return os.clock()end function Clock._keepTrackOfTime(J)J:_stopTrackingTime()J.initialClockTime=J:getCurrentTime()J.timeKeepingConnection=J.RunService.Heartbeat:Connect(function()local s=J:getCurrentTime()-J.initialClockTime J.timePosition.Value=s+J.initialTimePosition end)return end function Clock._stopTrackingTime(J)if J.timeKeepingConnection then J.timeKeepingConnection:Disconnect()J.timeKeepingConnection=nil end end function Clock.StartTime(J)(coroutine.wrap(function()J:_keepTrackOfTime()end))()end function Clock.ResetTime(s)s:_stopTrackingTime()s.timePosition.Value=J s.initialTimePosition=J end function Clock.SetTime(s,J)s:_stopTrackingTime()s.timePosition.Value=J s.initialTimePosition=J end return Clock
-- If at 0, audio player will start right away because audio starts at 0 time position
-- Open to alternatives
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,
	}
	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()
		local differenceSinceCall = self:getCurrentTime() - self.initialClockTime
		self.timePosition.Value = differenceSinceCall + self.initialTimePosition
	end)

	return
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
