TheHeckingDeveloper

Datastore

Apr 26th, 2021
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. -- Services
  2. local replicatedStorage = game:GetService("ReplicatedStorage")
  3. local serverStorage = game:GetService("ServerStorage")
  4. local dataStoreService = game:GetService("DataStoreService")
  5. local players = game:GetService("Players")
  6.  
  7. -- Datastores
  8. local analyticsStore = dataStoreService:GetDataStore("GameAnalytics")
  9.  
  10. -- Server
  11. local classes = serverStorage:WaitForChild("Classes")
  12. local configurations = serverStorage:WaitForChild("Configurations")
  13. local singletons = serverStorage:WaitForChild("Singletons")
  14.  
  15. -- Configurations
  16. local getGeneralAnalytics = require(configurations:WaitForChild("GeneralAnalytics"))
  17. local playerAnalytics = require(configurations:WaitForChild("PlayerAnalytics"))
  18.  
  19. -- Server modules
  20. local DSInterface = require(classes:WaitForChild("DSInterface"))
  21.  
  22. -- Singletons
  23. local analyticsManager = require(singletons:WaitForChild("AnalyticsManager"))
  24.  
  25. -- Modules
  26. local dataStore = require(classes.DataStore2)
  27.  
  28. -- Constants
  29. local START_YEAR = 2021
  30.  
  31. local KEY_STR = "DATE _ %s - %s"
  32.  
  33. -- Function cache
  34. local osTime = os.time
  35. local osDate = os.date
  36.  
  37. local stringLower = string.lower
  38. local stringFormat = string.format
  39.  
  40. local mathRandom = math.random
  41.  
  42. -- Tables
  43. local generalAnalytics = {
  44.     ActiveUsers = {
  45.         Day = 0,
  46.         Month = 0
  47.     },
  48.    
  49.     TotalUsers = 0
  50. }
  51.  
  52. -- Functions
  53. local function subGet(array, index)
  54.     return array[index] - 1
  55. end
  56.  
  57. -- Post-function variables
  58. local generalAnalytics = getGeneralAnalytics()
  59.  
  60. local function saveAnalytics()
  61.     analyticsManager:IncrementAnalytics(generalAnalytics)
  62.    
  63.     generalAnalytics = getGeneralAnalytics()
  64. end
  65.  
  66. -- Event connections
  67. players.PlayerAdded:Connect(function(plr)
  68.     local analyticsOBJ = dataStore("Analytics", plr)
  69.    
  70.     local getValue = analyticsOBJ:Get()
  71.     local analytics = getValue or playerAnalytics()
  72.    
  73.     local currentTime = osTime()
  74.     local currentDate = osDate("*t")
  75.    
  76.     currentDate.year -= START_YEAR
  77.    
  78.     local lastJoined = analytics.LastJoined
  79.     local activeUsers = generalAnalytics.ActiveUsers
  80.    
  81.     local isDifferent
  82.    
  83.     for timePeriod, value in pairs(lastJoined) do
  84.         -- timePeriod is "Day"
  85.         -- value is 0
  86.         local currentValue = currentDate[stringLower(timePeriod)] -- currentDate["day"] ( 1 )
  87.        
  88.         if isDifferent or (currentValue ~= value) then -- True, value is not the same as currentValue
  89.             print(timePeriod, value)
  90.             print(currentValue)
  91.            
  92.             activeUsers[timePeriod] += 1
  93.            
  94.             lastJoined[timePeriod] = currentValue -- lastJoined.Day = 1
  95.             isDifferent = true
  96.         end
  97.        
  98.         local firstStr = "Currently, the %s is: %s"
  99.         local secondStr = "The player last joined on the %s"
  100.     end
  101.    
  102.     if not analytics.HasJoined then
  103.         generalAnalytics.UniqueUsers += 1
  104.        
  105.         analytics.HasJoined = true
  106.     end
  107.    
  108.     analyticsOBJ:Set(analytics)
  109. end)
  110.  
  111. coroutine.wrap(function()
  112.     while true do
  113.         wait(12)
  114.        
  115.         saveAnalytics()
  116.        
  117.         wait(530 + mathRandom(1, 100))
  118.     end
  119. end)()
Advertisement
Add Comment
Please, Sign In to add comment