Advertisement
Guest User

Datastore

a guest
Aug 2nd, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. local DS = game:GetService("DataStoreService"):GetGlobalDataStore("PlayerDataStore")
  2.  
  3. function SaveData(Key, Value)
  4. DS:UpdateAsync(Key, function(OldValue)
  5. return Value
  6. end)
  7. end
  8.  
  9. function GetSaveTable(StatsModel)
  10. local R = {}
  11. for i,v in pairs(StatsModel:GetChildren()) do
  12. R[tostring(i.Name)] = v.Value
  13. end
  14. return R
  15. end
  16.  
  17. game.Players.PlayerAdded:connect(function(Player)
  18. local PlayerDataStore = DS:GetAsync(Player.userId)
  19. local stats = Instance.new("Model", game.ReplicatedStorage.PlayerStats)
  20.  
  21. -- Throttle DataStore updates
  22. local Request_WaitSecondsThreshold = 30 -- Save every 30 seconds
  23. local Request_QueueThreshold = 5 -- or save whenever 5 changes have happened
  24. local CurrentRequestQueue = 0
  25. -- The above is to make sure we don't hit the DataStore request limit
  26.  
  27. stats.Name = Player.Name
  28.  
  29. -- If they don't have a save, make one.
  30. if PlayerDataStore then
  31. PlayerDataStore = {Exp = 0, Gold = 0, Level = 1, Levelcap = 120, HP = 83, MaxExp = 83, Stamina = 100, MaxHP = 85}
  32. print("PDS made")
  33. end
  34.  
  35. for x,v in pairs(PlayerDataStore) do
  36. local Num = Instance.new("IntValue",stats)
  37. Num.Name = x
  38. Num.Value = v
  39. Num.Changed:connect(function(Exp)
  40. CurrentRequestQueue = CurrentRequestQueue + 1
  41. if (CurrentRequestQueue >= Request_QueueThreshold) then
  42. local Data = GetSaveTable(stats)
  43. CurrentRequestQueue = 0
  44. SaveData(Data)
  45. end
  46. end)
  47. end
  48. while (wait(Request_WaitSecondsThreshold)) do
  49. local Data = GetSaveTable(stats)
  50. CurrentRequestQueue = 0
  51. SaveData(Data)
  52. end
  53. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement