Advertisement
Quoteory

Data

Nov 14th, 2019
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.09 KB | None | 0 0
  1. local DataManager = {}
  2.  
  3. local DataStoreService = game:GetService("DataStoreService")
  4. local playerDataStore = DataStoreService:GetDataStore("DATASTORE17")
  5.  
  6. local sessionData = {}
  7.  
  8.  
  9. local function GetPlayerKey(player)
  10.     return "Player_" .. player.UserId  
  11. end
  12.  
  13. function DataManager:SetupPlayerData(player)
  14.     local playerKey = GetPlayerKey(player)
  15.    
  16.     local success, data = pcall(function()
  17.         return playerDataStore:GetAsync(playerKey)
  18.     end)
  19.    
  20.    
  21.     if success then
  22.         if data then
  23.             sessionData[playerKey] = data
  24.         else
  25.             sessionData[playerKey] = {Tools = {}}
  26.         end
  27.     else
  28.         warn(player.Name .. "'s Data failed to load")
  29.     end
  30. end
  31.  
  32. function DataManager:SavePlayerData(player)
  33.     local playerKey = GetPlayerKey(player)
  34.     if sessionData[playerKey] then
  35.         local success, err = pcall(function()
  36.             playerDataStore:SetAsync(playerKey, sessionData[playerKey])
  37.         end)
  38.        
  39.         if success then
  40.             print("saved")
  41.         else
  42.             warn(player.Name .. "'s data failed to save")
  43.         end
  44.     end
  45. end
  46.  
  47. function DataManager:GetPlayerData(player)
  48.     return sessionData[GetPlayerKey(player)]
  49. end
  50.  
  51. return DataManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement