Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. local data = game:GetService("DataStoreService"):GetOrderedDataStore("Candy")
  2.  
  3. local function SaveData(plr,t)
  4. local tries = 0
  5. local max = 5
  6. local success
  7. while not success and tries < max do
  8. success = pcall(function()
  9. if plr and t then
  10. data:SetAsync(plr.UserId,t.Value)
  11. end
  12. end)
  13. tries = tries + 1
  14. wait(tries)
  15. end
  16. end
  17.  
  18. game.Players.PlayerAdded:Connect(function(plr)
  19. local stat
  20. local function GetStats()
  21. wait(0.1)
  22. if not plr:FindFirstChild("Stats") then
  23. wait(1.5)
  24. GetStats()
  25. else
  26. stat = plr:WaitForChild("Stats")
  27. return true;
  28. end
  29. end
  30. GetStats()
  31. local function CreateStat(class,name,parent)
  32. local i = Instance.new(class)
  33. i.Name = tostring(name)
  34. i.Parent = parent
  35. return i
  36. end
  37. local money = CreateStat("IntValue","🍬Candys",stat)
  38. wait(0.03)
  39. local tries = 0
  40. local max = 5
  41. local success
  42. local SavedLevel
  43. while not success and tries < max do
  44. success = pcall(function()
  45. SavedLevel = data:GetAsync(plr.UserId)
  46. end)
  47. wait(tries)
  48. tries = tries + 1
  49. end
  50. if SavedLevel then
  51. money.Value = SavedLevel
  52. else
  53. SaveData(plr,money)
  54. end
  55.  
  56. local autoSaveInterval = 180
  57. coroutine.resume(coroutine.create(function()
  58. while wait(autoSaveInterval) do
  59. SaveData(plr,money)
  60. end
  61. end))
  62.  
  63. end)
  64. game.Players.PlayerRemoving:Connect(function(plr)
  65. if plr:FindFirstChild("Stats") and plr.Stats:FindFirstChild("🍬Candys") then
  66. SaveData(plr,plr.Stats["🍬Candys"])
  67. end
  68. end)
  69. if not game:GetService("RunService"):IsStudio() then
  70. game:BindToClose(function()
  71. for _,plr in pairs(game:GetService("Players")) do
  72. if plr:FindFirstChild("Stats") and plr.Stats:FindFirstChild("🍬Candys") then
  73. SaveData(plr,plr.Stats["🍬Candys"])
  74. end
  75. end
  76. wait(10)
  77. end)
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement