Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables
- local timeManager = {}
- local timeData = {}
- -- Services
- local coreModule = require(script:FindFirstAncestor("CoreModule"))
- local timeFormattingLibrary = coreModule:Get("Libraries.TimeFormattingLibrary")
- local players = coreModule.Services.Players
- -- Data
- local playerDataManager = coreModule:Get("GameModules.ServerGameManager.PlayerManager.PlayerDataManager")
- local orderedDataLibrary = coreModule:Get("Libraries.DataLibrary.OrderedDataLibrary")
- -- Remotes
- local leaderboardRemote = coreModule.SharedCore:Get("Libraries.RemoteLibrary"):GetRemote("GetTimes")
- local startTimeRemote = coreModule.SharedCore:Get("Libraries.RemoteLibrary"):GetRemote("StartTime")
- -- Methods
- function timeManager:find(tbl, value, key)
- if key then
- for k, v in pairs(tbl) do
- if v[key] == value then
- return v, k
- end
- end
- else
- return table.find(tbl, value)
- end
- end
- function timeManager:StartTiming(player)
- local playerData = playerDataManager:GetData(player)
- if playerData.Values.TimeMode == true then return end
- print("started timing")
- playerData.Values.TimeMode = true
- timeData[#timeData + 1] = {userId = player.UserId, startTime = tick(), totalTime = nil}
- end
- function timeManager:StopTiming(player, optional)
- local playerData = playerDataManager:GetData(player)
- local index = timeManager:find(timeData, player.UserId, 'userId')
- if not index then return end
- if optional then
- playerData.Values.LatestCheckpoint = 1
- playerData.Values.TimeMode = false
- startTimeRemote:FireClient(player)
- table.remove(timeData, #index)
- return
- end
- print("finished timing")
- playerData.Values.LatestCheckpoint = 1
- index["totalTime"] = tick() - index["startTime"]
- startTimeRemote:FireClient(player)
- playerData.Values.TimeMode = false
- end
- function timeManager:UpdateTimeLeaderboard(id, value)
- print("updating leaderboard")
- orderedDataLibrary:SetAsync("_TimeLeaderboard", id, value)
- end
- -- Initialize
- function timeManager:Initialize()
- coroutine.wrap(function()
- if #players:GetPlayers() == 0 then
- players.PlayerAdded:Wait()
- end
- wait(1)
- while true do
- pcall(function()
- for index, data in pairs (timeData) do
- local id, total = data.userId, data.totalTime
- if total == nil then
- print("not done", id, total)
- -- player did not complete it
- else
- print("done", id, total)
- local timeToComplete = timeFormattingLibrary:FormatSecondsToHMS(total*60)
- print(timeToComplete)
- print("a")
- timeManager:UpdateTimeLeaderboard(id, total)
- print("b")
- table.remove(timeData, index)
- end
- end
- local timeData = orderedDataLibrary:GetSortedAsync("_TimeLeaderboard", false, 18):GetCurrentPage()
- leaderboardRemote:FireAllClients(timeData)
- end)
- wait(180)
- end
- end)()
- end
- --
- return timeManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement