Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. -- Variables
  2. local timeManager = {}
  3. local tempData = {}
  4.  
  5. -- Services
  6. local coreModule = require(script:FindFirstAncestor("CoreModule"))
  7. local Players = coreModule.Services.Players
  8. local playerDataManager = coreModule:Get("GameModules.ServerGameManager.PlayerManager.PlayerDataManager")
  9. local orderedDataLibrary = coreModule:Get("Libraries.DataLibrary.OrderedDataLibrary")
  10. local leaderboardRemote = coreModule.SharedCore:Get("Libraries.RemoteLibrary"):GetRemote("GetLeaderboard")
  11. local startTimeRemote = coreModule.SharedCore:Get("Libraries.RemoteLibrary"):GetRemote("StartTime")
  12.  
  13. local function find(tbl, value, key)
  14.     if key then
  15.         for k, v in pairs(tbl) do
  16.             if v[key] == value then
  17.                 return v, k
  18.             end
  19.         end
  20.     else
  21.         return table.find(tbl, value)
  22.     end
  23. end
  24.  
  25. function format(Int)
  26.     return string.format("%02i", Int)
  27. end
  28.  
  29. function convertToHMS(Seconds)
  30.     local Minutes = (Seconds - Seconds%60)/60
  31.     Seconds = Seconds - Minutes*60
  32.     local Hours = (Minutes - Minutes%60)/60
  33.     Minutes = Minutes - Hours*60
  34.     return format(Hours)..":"..format(Minutes)..":"..format(Seconds)
  35. end
  36.  
  37. function timeManager:UpdateLeaderboard(player, datastore, value)
  38.     orderedDataLibrary:SetAsync(datastore, player.UserId, value)
  39. end
  40.  
  41. function timeManager:StartTiming(player)
  42.     local playerData = playerDataManager:GetData(player)
  43.     tempData[#tempData + 1] = {player = player.UserId, starttime = tick(), totaltime = nil}
  44.     playerData.Values.TimeMode = true
  45. end
  46.  
  47. function timeManager:StopTiming(player, optional)
  48.     local playerData = playerDataManager:GetData(player)
  49.     local index = find(tempData, player.UserId, 'player')
  50.    
  51.     if optional then
  52.         playerData.Values.TimeMode = false
  53.         startTimeRemote:FireClient(player)
  54.         if index then
  55.             table.remove(tempData, #index)
  56.         end
  57.         return
  58.     end
  59.    
  60.     if index then
  61.         index["totaltime"] = tonumber(index["starttime"]) - tick()
  62.         print(index["totaltime"])
  63.         playerData.Values.TimeMode = false
  64.         startTimeRemote:FireClient(player)
  65.     end
  66. end
  67.  
  68. -- Initialize
  69. function timeManager:Initialize()
  70.     coroutine.wrap(function()
  71.         if #Players:GetPlayers() == 0 then
  72.             Players.PlayerAdded:Wait()
  73.         end
  74.  
  75.         wait(1)
  76.        
  77.         while true do
  78.             pcall(function()
  79.                 for index, data in pairs (tempData) do
  80.                     local id, total = data.player, data.totaltime
  81.                     if total == nil then
  82.                         -- they never completed it
  83.                     else
  84.                         local timeToComplete = convertToHMS(total*60)
  85.                         timeManager:UpdateLeaderboard(id, "__Time", timeToComplete)
  86.                         table.remove(tempData, index)
  87.                     end
  88.                 end
  89.                
  90.                 local StageData = orderedDataLibrary:GetSortedAsync("__Checkpoints", false, 18):GetCurrentPage()
  91.                 local TimeData = orderedDataLibrary:GetSortedAsync("__Time", false, 18):GetCurrentPage()
  92.                 for _, data in pairs (TimeData) do print(table.unpack(data)) end
  93.                 leaderboardRemote:FireAllClients(StageData, TimeData)
  94.             end)
  95.             wait(180)
  96.         end
  97.     end)()
  98. end
  99.  
  100. --
  101. return timeManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement