SHARE
TWEET

Data

a guest Dec 13th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Services
  2. local DataStoreService = game:GetService("DataStoreService")
  3. local Players = game:GetService("Players")
  4. --Services
  5.  
  6. --Require
  7. local Event = require(game.ReplicatedStorage.GenericModules.Event)
  8. --Require
  9.  
  10. --Declarations
  11. local datastore = DataStoreService:GetDataStore("0")
  12. --Declarations
  13.  
  14. local Data =
  15. {
  16.     NewPlayerData = Event.new();
  17.     PlayerDataLoaded = Event.new();
  18.     UnloadingPlayerData = Event.new();
  19. }
  20. setmetatable(Data,
  21. {
  22.     __index = function(key)
  23.         pcall(function()
  24.             local data = DataStoreService:GetDataStore(key)
  25.             Data[key] = data
  26.             return data
  27.         end)
  28.     end
  29. })
  30.  
  31. --Connections
  32. local function PlayerAdded(player)
  33.     local data
  34.     if not pcall(function()
  35.         data = datastore:GetAsync(player.UserId)
  36.     end) then
  37.         player:Kick("Your data failed to load. :/")
  38.     end
  39.     if not data then
  40.         data = {}
  41.         Data.NewPlayerData:Fire(player, data)
  42.     end
  43.     Data.PlayerDataLoaded:Fire(player, data)
  44.     Data[player] = data
  45. end
  46. Players.PlayerAdded:Connect(PlayerAdded)
  47.  
  48. for k, player in pairs(Players:GetPlayers()) do
  49.     PlayerAdded(player)
  50. end
  51.  
  52. function PlayerRemoving(player)
  53.     local data = Data[player]
  54.     Data.UnloadingPlayerData:Fire(player, data)
  55.     datastore:SetAsync(player.UserId, data)
  56.     Data[player] = nil
  57. end
  58. Players.PlayerRemoving:Connect(PlayerRemoving)
  59. --Connections
  60.  
  61. return Data
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