Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. local DSService = game:GetService('DataStoreService'):GetDataStore('NAME')
  2. ID = 6
  3. SaveTime = 300
  4. leaderboardname = "Stats"
  5. local numberOfRetries = 5
  6.  
  7. StatList = { }
  8.  
  9. function CreateValue(Name, Parent, Value)
  10. Value = Value or 0
  11. local savevalue = Instance.new('IntValue', Parent)
  12. savevalue.Name = Name
  13. savevalue.Value = Value
  14. return savevalue
  15. end
  16.  
  17. function GetValue(uniquekey)
  18. -- GetAsync
  19. local count = 0
  20. local Success, GetSaved repeat
  21. Success, GetSaved = pcall(function() return DSService:GetAsync(uniquekey) end)
  22. count = count + 1
  23. wait(1.5)
  24. until Success or count == numberOfRetries
  25. if Sucess and GetSaved then
  26. return GetSaved
  27. end
  28. return nil
  29. end
  30.  
  31. game.Players.PlayerAdded:connect(function(plr)
  32. local uniquekey = ID..plr.userId
  33. local Levelss = Instance.new('IntValue', plr)
  34. Levelss.Name = leaderboardname
  35. for i,v in pairs(StatList) do
  36. local Stat = GetValue(i..uniquekey)
  37. if Stat then
  38. CreateValue(i, Levelss, Stat) --Old player load their value
  39. else
  40. CreateValue(i, Levelss, v) --New player load the default value
  41. end
  42. end
  43. end)
  44.  
  45. game.Players.PlayerRemoving:connect(function(plr)
  46. local uniquekey = ID..plr.userId
  47. if plr:FindFirstChild(leaderboardname) then
  48. for i,v in pairs(StatList) do
  49. if plr[leaderboardname]:FindFirstChild(i) then
  50. DSService:SetAsync(i..uniquekey, plr[leaderboardname][i].Value)
  51. end
  52. end
  53. end
  54. end)
  55.  
  56. while wait(SaveTime) do
  57. P = game:GetService("Players"):GetChildren()
  58. for o=1, #P do
  59. if P[o] then
  60. local uniquekey = ID..P[o].userId
  61. if P[o]:FindFirstChild(leaderboardname) then
  62. for i,v in pairs(StatList) do
  63. if P[o][leaderboardname]:FindFirstChild(i) then
  64. local count = 0
  65. local Success repeat
  66. Success = pcall(function() DSService:SetAsync(i..uniquekey, P[o][leaderboardname][i].Value) end)
  67. count = count + 1
  68. wait(1.5)
  69. until Success or count == numberOfRetries
  70. end
  71. end
  72. end
  73. end
  74. end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement