Advertisement
Guest User

very Incomplete Datastore

a guest
Mar 30th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1. --// Services \\--
  2. local Players = game:GetService("Players")
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4. local DataStoreService = game:GetService("DataStoreService")
  5. local RunService = game:GetService("RunService")
  6.  
  7. --// Modules \\--
  8. local Settings = require(script.Settings)
  9.  
  10. local Datastore = DataStoreService:GetDataStore("Datastore #----")
  11.  
  12. --// Tables \\--
  13. local Starting_Data = {
  14.     Data = {
  15.         Stats = {
  16.             Strength = "BEEP BEEP",
  17.         },
  18.     },
  19. }
  20.  
  21. local Servers_Data = {}
  22. local Servers_Log = {}
  23.  
  24. --// Functions \\--
  25.  
  26. local function Data_Attempt(Player, Function, Argument)
  27.     local Success, Error, Attempt, Data = nil, nil, 0, nil
  28.    
  29.     while Attempt < Settings.MaxAttempts and not Success do
  30.         Attempt = Attempt + 1
  31.         Success, Error = pcall(function()
  32.             Data = Function(Player, Argument)
  33.         end)
  34.         wait()
  35.     end
  36.    
  37.     --[[repeat wait()
  38.         Attempt = Attempt + 1
  39.         Success, Error = pcall(function()
  40.             Data = Function(Player, Argument)
  41.         end)
  42.     until Success or Attempt >= Settings.MaxAttempts do end
  43.     --]]
  44.     if Success then
  45.         print("[Database] The function: |"..Function.."| has successfully been called for the player: |"..Player.Name.."|")
  46.     else
  47.         warn("[Database] The function: |"..Function.."| has unsuccessfully been called for the player: |"..Player.Name.."|")
  48.     end
  49.     return Data
  50. end
  51.  
  52. local function Save(Player, Data)
  53.     Datastore:SetAsync(Player.UserId, Data)
  54. end
  55.  
  56. local function GetData(Player)
  57.     local Data = Datastore:GetAsync(Player.UserId)
  58.     return Data
  59. end
  60.  
  61. --// Modules 2 \\--
  62. local module = {
  63.     NewPlayer = function(Player)
  64.         local Players_Data = Data_Attempt(Player, GetData)
  65.        
  66.         if Players_Data == nil then
  67.             Servers_Data[Player].Data = Starting_Data.Data
  68.             print("[Database] The player: |"..Player.Name.."| had no pre-existing data, they have been awarded the default data.")
  69.         elseif Players_Data then
  70.             Servers_Data[Player].Data = Players_Data
  71.             print("[Database] The player: |"..Player.Name.."| had pre-existing data, they have been awarded their last data save.")
  72.         end
  73.     end
  74. }
  75.  
  76. --// Player Control \\--
  77. Players.PlayerAdded:Connect(function(Player)
  78.     Servers_Data[Player] = {}
  79.     if RunService:IsStudio() and not Settings.StudioLoad then return end
  80.     module.NewPlayer(Player)
  81. end)
  82.  
  83. Players.PlayerRemoving:Connect(function(Player)
  84.     if RunService:IsStudio() and not Settings.StudioSave then return end
  85.     Data_Attempt(Player, Save, Servers_Data[Player].Data)
  86.     print("[Database] The player: |"..Player.Name.."| has left the game.")
  87. end)
  88.  
  89. game:BindToClose(function()
  90.     if RunService:IsStudio() and not Settings.StudioSave then return end
  91.     local Player_List = Players:GetPlayers()
  92.     for i = 1,#Player_List do
  93.         local Player = Player_List[i]
  94.         print("[Database] The player: |"..Player.Name.."| is having their data safely saved.")
  95.         Data_Attempt(Player, Save, Servers_Data[Player].Data)
  96.     end
  97. end)
  98.  
  99. return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement