Advertisement
qsenko1

MainStatsDatastore

Nov 7th, 2022
1,398
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.33 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local RunService = game:GetService("RunService")
  3. local DataStoreService = game:GetService("DataStoreService")
  4. local MainStatsDatastore = DataStoreService:GetDataStore("MainStats") -- GearsData
  5.  
  6. local function waitForRequestBudget(requestType)
  7.     local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
  8.     while currentBudget < 1 do
  9.         currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
  10.         task.wait(5)
  11.     end
  12. end
  13.  
  14. game.Players.PlayerAdded:Connect(function(Player)
  15.     if Player then
  16.         local leaderstats = Instance.new("Folder",Player)
  17.         leaderstats.Name = "leaderstats"
  18.         local Wins = Instance.new("IntValue",leaderstats)
  19.         Wins.Name = "Wins"
  20.         local Coins = Instance.new("IntValue",leaderstats)
  21.         Coins.Name = "Coins"
  22.         local Gems = Instance.new("IntValue",leaderstats)
  23.         Gems.Name = "Gems 💎"
  24.         local ValuesFolder = Instance.new("Folder",Player)
  25.         ValuesFolder.Name = "ValuesFolder"
  26.         local StagesCompleted = Instance.new("IntValue",ValuesFolder)
  27.         StagesCompleted.Name = "Stages Completed"
  28.         local CoinsEarned = Instance.new("IntValue",ValuesFolder)
  29.         CoinsEarned.Name = "Coins Earned"
  30.         local TimePlayed = Instance.new("IntValue",ValuesFolder)
  31.         TimePlayed.Name = "Time Played"
  32.         local Deaths = Instance.new("IntValue",ValuesFolder)
  33.         Deaths.Name = "Deaths"
  34.        
  35.         local userId = Player.UserId
  36.         local key = "Player_"..userId
  37.        
  38.         local success, returnValue
  39.         success, returnValue = pcall(MainStatsDatastore.GetAsync, MainStatsDatastore, key)
  40.        
  41.         if success then
  42.             if returnValue == nil then
  43.                 returnValue = {
  44.                     Wins = 0,
  45.                     Coins = 0,
  46.                     Gems = 0,
  47.                     StagesCompleted = 0,
  48.                     CoinsEarned = 0,
  49.                     TimePlayed = 0,
  50.                     Deaths = 0,            
  51.                 }
  52.             end
  53.  
  54.             Wins.Value = if returnValue.Wins ~= nil then returnValue.Wins else 0
  55.             Coins.Value = if returnValue.Coins ~= nil then returnValue.Coins else 0
  56.             Gems.Value = if returnValue.Gems ~= nil then returnValue.Gems else 0
  57.             StagesCompleted.Value = if returnValue.StagesCompleted ~= nil then returnValue.StagesCompleted else 0
  58.             CoinsEarned.Value = if returnValue.CoinsEarned ~= nil then returnValue.CoinsEarned else 0
  59.             TimePlayed.Value = if returnValue.TimePlayed ~= nil then returnValue.TimePlayed else 0
  60.             Deaths.Value = if returnValue.Deaths ~= nil then returnValue.Deaths else 0
  61.         else
  62.             Player:Kick("There was an error with loading your Data! Roblox's Datastore might be down,try agen later or contact us trough our Group!")
  63.             print(Player.Name.. " Has a Data loading ERROR!!")
  64.         end
  65.        
  66.     end
  67. end)
  68.  
  69. game.Players.PlayerRemoving:Connect(function(Player)
  70.     local userId = Player.UserId
  71.     local key = "Player_"..userId
  72.    
  73.     local wins = Player.leaderstats.Wins.Value
  74.     local coins = Player.leaderstats.Coins.Value
  75.     local gems = Player.leaderstats:FindFirstChild("Gems 💎").Value
  76.     local stagescompleted = Player.ValuesFolder:FindFirstChild("Stages Completed").Value
  77.     local coinsearned = Player.ValuesFolder:FindFirstChild("Coins Earned").Value
  78.     local timePlayed = Player.ValuesFolder:FindFirstChild("Time Played").Value
  79.     local deaths = Player.ValuesFolder.Deaths.Value
  80.    
  81.     local DataTable = {
  82.         Wins = wins,
  83.         Coins = coins,
  84.         Gems = gems,
  85.         StagesCompleted = stagescompleted,
  86.         CoinsEarned = coinsearned,
  87.         TimePlayed = timePlayed,
  88.         Deaths = deaths,
  89.     }
  90.    
  91.     print(DataTable)
  92.  
  93.     local success,returnValue
  94.     success,returnValue = pcall(MainStatsDatastore.UpdateAsync, MainStatsDatastore, key, function()
  95.         return DataTable
  96.     end)
  97.    
  98.     if success  then
  99.         print("MainStats Saved!")
  100.     else
  101.         print("Data Saving ERROR!!")
  102.     end
  103.    
  104. end)
  105.  
  106. function SetupPlayerData(Player)
  107.     local leaderstats = Instance.new("Folder",Player)
  108.     leaderstats.Name = "leaderstats"
  109.     local Wins = Instance.new("IntValue",leaderstats)
  110.     Wins.Name = "Wins"
  111.     local Coins = Instance.new("IntValue",leaderstats)
  112.     Coins.Name = "Coins"
  113.     local Gems = Instance.new("IntValue",leaderstats)
  114.     Gems.Name = "Gems 💎"
  115.     local ValuesFolder = Instance.new("Folder",Player)
  116.     ValuesFolder.Name = "ValuesFolder"
  117.     local StagesCompleted = Instance.new("IntValue",ValuesFolder)
  118.     StagesCompleted.Name = "Stages Completed"
  119.     local CoinsEarned = Instance.new("IntValue",ValuesFolder)
  120.     CoinsEarned.Name = "Coins Earned"
  121.     local TimePlayed = Instance.new("IntValue",ValuesFolder)
  122.     TimePlayed.Name = "Time Played"
  123.     local Deaths = Instance.new("IntValue",ValuesFolder)
  124.     Deaths.Name = "Deaths"
  125.  
  126.     local userId = Player.UserId
  127.     local key = "Player_"..userId
  128.  
  129.     local success, returnValue
  130.     repeat
  131.     waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)   
  132.     success, returnValue = pcall(MainStatsDatastore.GetAsync, MainStatsDatastore, key)
  133.     until success or not Players:FindFirstChild(Player.Name)
  134.    
  135.     if success then
  136.         if returnValue == nil then
  137.             returnValue = {
  138.                 Wins = 0,
  139.                 Coins = 0,
  140.                 Gems = 0,
  141.                 StagesCompleted = 0,
  142.                 CoinsEarned = 0,
  143.                 TimePlayed = 0,
  144.                 Deaths = 0,            
  145.             }
  146.         end
  147.  
  148.         Wins.Value = if returnValue.Wins ~= nil then returnValue.Wins else 0
  149.         Coins.Value = if returnValue.Coins ~= nil then returnValue.Coins else 0
  150.         Gems.Value = if returnValue.Gems ~= nil then returnValue.Gems else 0
  151.         StagesCompleted.Value = if returnValue.StagesCompleted ~= nil then returnValue.StagesCompleted else 0
  152.         CoinsEarned.Value = if returnValue.CoinsEarned ~= nil then returnValue.CoinsEarned else 0
  153.         TimePlayed.Value = if returnValue.TimePlayed ~= nil then returnValue.TimePlayed else 0
  154.         Deaths.Value = if returnValue.Deaths ~= nil then returnValue.Deaths else 0
  155.     else
  156.         Player:Kick("There was an error with loading your Data! Roblox's Datastore might be down,try agen later or contact us trough our Group!")
  157.         print(Player.Name.. " Has a Data loading ERROR!!")
  158.     end
  159. end
  160.  
  161. function Save(Player)
  162.     local userId = Player.UserId
  163.     local key = "Player_"..userId
  164.  
  165.     local wins = Player.leaderstats.Wins.Value
  166.     local coins = Player.leaderstats.Coins.Value
  167.     local gems = Player.leaderstats:FindFirstChild("Gems 💎").Value
  168.     local stagescompleted = Player.ValuesFolder:FindFirstChild("Stages Completed").Value
  169.     local coinsearned = Player.ValuesFolder:FindFirstChild("Coins Earned").Value
  170.     local timePlayed = Player.ValuesFolder:FindFirstChild("Time Played").Value
  171.     local deaths = Player.ValuesFolder.Deaths.Value
  172.  
  173.     local DataTable = {
  174.         Wins = wins,
  175.         Coins = coins,
  176.         Gems = gems,
  177.         StagesCompleted = stagescompleted,
  178.         CoinsEarned = coinsearned,
  179.         TimePlayed = timePlayed,
  180.         Deaths = deaths,
  181.     }
  182.  
  183.     print(DataTable)
  184.  
  185.     local success,returnValue
  186.     repeat
  187.        
  188.     waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
  189.     success,returnValue = pcall(MainStatsDatastore.UpdateAsync, MainStatsDatastore, key, function()
  190.         return DataTable
  191.     end)
  192.     until success
  193.     if success  then
  194.         print("MainStats Saved!")
  195.     else
  196.         print("Data Saving ERROR!!")
  197.     end
  198. end
  199.  
  200. function OnShutdown()
  201.     if RunService:IsStudio()then
  202.         task.wait(2)
  203.     else
  204.         local finished = Instance.new("BindableEvent")
  205.         local allPlayers = Players:GetPlayers()
  206.         local leftPlayers = #allPlayers
  207.        
  208.         for _, player in ipairs(allPlayers) do
  209.             coroutine.wrap(function()
  210.                 Save(player)
  211.                 leftPlayers -= 1
  212.                 if leftPlayers == 0 then
  213.                     finished:Fire()
  214.                 end
  215.             end)()
  216.         end
  217.         finished.Event:Wait()
  218.     end
  219. end
  220.  
  221. for _, player in ipairs(game.Players:GetPlayers()) do
  222.     coroutine.wrap(SetupPlayerData)(player)
  223. end
  224.  
  225. game:BindToClose(OnShutdown)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement