-- gets the datastore, fails if could not find

local TimePlayedDatabase = nil
local suc, err = pcall(function ()
	TimePlayedDatabase = game:GetService("DataStoreService"):GetOrderedDataStore(TIME_PLAYED_DATA_STORE)
end)
if not suc or TimePlayedDatabase == nil then warn(err) script.Parent:Destroy() end

module.TimePlayedResult = nil
module.MostTipsResult = nil

module.GetTimePlayedResult = function ()
	return module.TimePlayedResult
end

module.GetTipsResult = function ()
	return module.MostTipsResult
end

module.UpdateAsync = function ()
	module.TimePlayedResult = TimePlayedDatabase:GetSortedAsync(false, 10, 1):GetCurrentPage()
end

module.StartLoop = function ()
	local suc, err = coroutine.resume(coroutine.create (function ()
		while wait(SCORE_UPDATE*(60-3)) do
			local players = game:GetService("Players"):GetPlayers()
			for _, player in pairs(players) do
				local stat = NAME_OF_STAT .. player.UserId
				local newval = TimePlayedDatabase:IncrementAsync(stat, SCORE_UPDATE)
				if DO_DEBUG then print("Incremented time played stat of", player, stat, "to", newval) end
			end
			module.UpdateAsync()
		end
	end))
	if not suc then warn(err) end
end
return module
