Advertisement
Kurokku

Untitled

May 27th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. ds = game:GetService'DataStoreService':GetDataStore('Data-1')
  2. folder = game.ReplicatedStorage:FindFirstChild('PlayerData'):FindFirstChild('DataTemplate')
  3.  
  4. local loadedOk = setmetatable({}, {__mode = 'k'});
  5.  
  6. function save(plr, Type)
  7. local key = tostring(plr.userId)
  8. local data = {}
  9. for iii,v in pairs(game.ReplicatedStorage:FindFirstChild('PlayerData'):FindFirstChild(plr.Name):GetChildren())do
  10. data[tostring(v)] = v.Value
  11. end
  12. local success, message = pcall(function()
  13. ds:UpdateAsync(key, function()
  14. return data
  15. end)
  16. end)
  17. if not success then
  18. print(plr.Name.." An error occurred: " .. message)
  19. return false
  20. else
  21. if Type == "Leaving" then
  22. game.ReplicatedStorage:FindFirstChild('PlayerData'):FindFirstChild(plr.Name):Destroy()
  23. return true
  24. else
  25. return true
  26. end
  27. end
  28. end
  29.  
  30. game.Players.PlayerAdded:Connect(function(plr)
  31. wait(0.1)
  32. local data
  33. local key = tostring(plr.userId)
  34. --
  35. local f = folder:clone()
  36. f.Name = tostring(plr)
  37. f.Parent = game.ReplicatedStorage:FindFirstChild('PlayerData')
  38.  
  39. data = ds:GetAsync(key) or {}
  40. for i,v in pairs(data)do
  41. if f:FindFirstChild(i) then
  42. f:FindFirstChild(i).Value = v
  43. end
  44. end
  45. loadedOk[plr] = true;
  46. end)
  47.  
  48. game:GetService'Players'.PlayerRemoving:Connect(function(plr)
  49. if (loadedOk[plr]) then
  50. if save(plr, "Leaving") == false then
  51. print("Failed to save "..plr.Name.."s data")
  52. end
  53. end
  54. end)
  55.  
  56. game:BindToClose(function()
  57. wait(10)
  58. end)
  59.  
  60. while wait(120) do
  61. for _,plr in pairs(game.Players:GetPlayers()) do
  62. if (loadedOk[plr]) then
  63. if save(plr, "AutoSave") == false then
  64. print("Failed to save "..plr.Name.."s data")
  65. end
  66. end
  67. end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement