SHARE
TWEET

Untitled

a guest Feb 14th, 2020 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --// Services
  2. local Players = game:GetService("Players")
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4. local RunService = game:GetService("RunService")
  5. local DSS = game:GetService("DataStoreService")
  6.  
  7. --// Variables
  8. local Version = 1
  9. local MaxTries = 5
  10.  
  11. local DataStore = DSS:GetDataStore("Player's Data: "..Version)
  12.  
  13. --// Settings
  14. local StartingData = {
  15.     Stats = {
  16.         Level = 1,
  17.         Exp = 0,
  18.         MaxExp = 100,
  19.         Money = 0,    
  20.     }    
  21. }
  22.  
  23. local ServerData = {}
  24.  
  25. --// Functions
  26. local Save = function(Player, DataToSave)
  27.     DataStore:SetAsync(Player.UserId, DataToSave)
  28.     print("Saving "..Player.Name.."'s Data")
  29. end
  30.  
  31. local Get = function(Player)
  32.     local Data = DataStore:GetAsync(Player.UserId)
  33.     print("Returning "..Player.Name.."'s Data")
  34.     return Data
  35. end
  36.  
  37. local DataRetry = function(Player, Function, Task)
  38.     local success, errormsg, Data, Tries = false, nil, nil, 0
  39.    
  40.     while Tries < MaxTries and not success do
  41.         Tries = Tries +1
  42.         success, errormsg = pcall(function()
  43.             Data = Function(Player, Task)
  44.         end)
  45.         if success then
  46.             print("Data Retry has been completed for: ")
  47.         else
  48.             warn(errormsg)
  49.             wait(1)
  50.         end
  51.     end
  52.     return Data
  53. end
  54.  
  55. --// Module
  56. local module = {
  57.     NewPlayer = function(Player)
  58.         local Data = DataRetry(Player, Get)
  59.        
  60.         if Data == nil then
  61.             ServerData[Player].Stats = StartingData.Stats
  62.             print(Player.Name.." has no Data")
  63.         elseif Data then
  64.             ServerData[Player].Stats = Data
  65.             print(Player.Name.."'s Data has been loaded")
  66.         end
  67.        
  68.     end,
  69.    
  70. }
  71.  
  72. --// Main
  73. Players.PlayerAdded:Connect(function(Player)
  74.     ServerData[Player] = {}
  75.     module.NewPlayer(Player)
  76. end)
  77. Players.PlayerRemoving:Connect(function(Player)
  78.     DataRetry(Player, Save, ServerData[Player].Stats)
  79.     ServerData[Player] = nil
  80. end)
  81.  
  82. return module
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top