Advertisement
Guest User

MewsData

a guest
Jul 23rd, 2020
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 KB | None | 0 0
  1. local module = {}
  2.  
  3. module.DataStoreName = "Data1"
  4.  
  5. module.Save_LoadInStudio = true
  6.  
  7. module.DataStore = game:GetService("DataStoreService"):GetDataStore(module.DataStoreName)
  8.  
  9. if game.Players.LocalPlayer then warn("DataStore requested on the client, is a server-only module.") return end
  10.  
  11.  
  12. function module.Load_Table_Data(Player,ObjectsToLoad)
  13.    
  14.    
  15.    
  16.     if type(Player) ~= "userdata" then error("Bad argument #1 to LoadData (player object expected) got "..type(Player)) return end
  17.        
  18.    
  19.     if type(ObjectsToLoad) ~= "table" then error("Bad argument #2 to LoadData (Table expected) Got "..type(ObjectsToLoad)) return end
  20.    
  21.    
  22.     local tries = 0
  23.     local s
  24.     local e
  25.     local data
  26.    
  27.     repeat
  28.         tries = tries + 1
  29.         s, e = pcall(function()
  30.             data = module.DataStore:GetAsync(Player.UserId)
  31.         end)
  32.        
  33.     until tries == 3 or s
  34.    
  35.     if e and module.Save_LoadInStudio then
  36.         warn("data did not load! module.Load_Table_Data  make sure you have Studio Access to API Services on")
  37.         return
  38.     end
  39.    
  40.     if data then
  41.         for i , v in pairs(ObjectsToLoad) do
  42.             v.Value = data[v.Name]
  43.         end
  44.     end
  45.    
  46.    
  47. end
  48.  
  49.  
  50.  
  51. function module.Load_Value(Player,object)
  52.  
  53.    
  54.     if type(Player) ~= "userdata" then error("Bad argument #1 to LoadData (player object expected) got "..type(Player)) return end
  55.    
  56.     if type(object) ~= "userdata" then error("Bad argument #1 to LoadData (Value object expected) got "..type(object)) return end
  57.    
  58.    
  59.     local tries = 0
  60.     local s
  61.     local e
  62.     local data
  63.    
  64.     repeat
  65.         tries = tries + 1
  66.         s, e = pcall(function()
  67.             data = module.DataStore:GetAsync(Player.UserId)
  68.         end)
  69.        
  70.     until tries == 3 or s
  71.    
  72.     if data then
  73.         object.Value = data
  74.     end
  75.    
  76.     if e and module.Save_LoadInStudio then
  77.         warn("data did not load! module.Load_Value  make sure you have Studio Access to API Services on")
  78.         return
  79.     end
  80.    
  81.    
  82. end
  83.  
  84.  
  85. function module.Save_Table_Data(Player,ObjectsToSave)
  86.     if type(Player) ~= "userdata" then error("Bad argument #1 to LoadData (player object expected) got "..type(Player)) return end
  87.        
  88.    
  89.     if type(ObjectsToSave) ~= "table" then error("Bad argument #2 to LoadData (Table expected) Got "..type(ObjectsToSave)) return end
  90.    
  91.    
  92.     local tries = 0
  93.     local s
  94.     local e
  95.    
  96.     repeat
  97.         tries = tries + 1
  98.        
  99.         s , e = pcall(function()
  100.             module.DataStore:UpdateAsync(Player.UserId,function(data)
  101.                 data={}
  102.                 for i ,v in pairs(ObjectsToSave) do
  103.                     data[v.Name] = v.Value
  104.                 end
  105.                 return data
  106.             end)
  107.         end)
  108.        
  109.        
  110.     until tries == 3 or s
  111.    
  112.     if e and module.Save_LoadInStudio then
  113.         warn("data did not save! module.Save_Table_Data  make sure you have Studio Access to API Services on")
  114.         return
  115.     end
  116.    
  117. end
  118.  
  119.  
  120.  
  121.  
  122. function module.Save_Value_Data(Player,object)
  123.     if type(Player) ~= "userdata" then error("Bad argument #1 to LoadData (player object expected) got "..type(Player)) return end
  124.        
  125.    
  126.     if type(object) ~= "userdata" then error("Bad argument #1 to LoadData (Value object expected) got "..type(object)) return end
  127.    
  128.    
  129.     local tries = 0
  130.     local s
  131.     local e
  132.    
  133.     repeat
  134.         tries = tries + 1
  135.        
  136.         s , e = pcall(function()
  137.             module.DataStore:UpdateAsync(Player.UserId,function(data)
  138.                 data = object.Value
  139.                 return data
  140.             end)
  141.         end)
  142.        
  143.        
  144.     until tries == 3 or s
  145.    
  146.     if e and module.Save_LoadInStudio then
  147.         warn("data did not save! module.Save_Value_Data  make sure you have Studio Access to API Services on")
  148.         return
  149.     end
  150.    
  151. end
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160. return module
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement