Advertisement
Kurokku

Untitled

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