Advertisement
Guest User

im dumb

a guest
Oct 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. function onPlayerEntered(newPlayer)
  2. local datafolder = Instance.new("Folder")
  3. datafolder.Parent = game.ReplicatedStorage.DataHolder
  4. datafolder.Name = newPlayer.UserId
  5. local vs = game.ReplicatedStorage.DataHolder.Data:clone()
  6. vs.Parent = game.ReplicatedStorage.DataHolder[newPlayer.UserId]
  7. local Data = vs:GetChildren()
  8. local S1 = {}
  9. for i=1,#Data do
  10. local n = Data[i].Name
  11. S1[n] = Data[i].Value
  12. end
  13. --Get Data--
  14. if newPlayer.Name ~= "Player" then
  15. local DataStore = game:GetService("DataStoreService"):GetDataStore(newPlayer.UserId)
  16. if DataStore:GetAsync("Data") == nil then
  17. DataStore:SetAsync("Data", S1)
  18. end
  19. S1 = DataStore:GetAsync("Data")
  20. end
  21. --Safely Load Data--
  22. local vv = vs:GetChildren()
  23. for i=1,#vv do
  24. local n = vv[i].Name
  25. --print(S1[n])
  26. if S1[n] ~= nil then
  27. vv[i].Value = S1[n]
  28. end
  29. end
  30. end
  31.  
  32. game.Players.PlayerAdded:Connect(onPlayerEntered)
  33.  
  34. for _,player in pairs(game.Players:GetChildren()) do
  35. onPlayerEntered(player)
  36. end
  37.  
  38. game.Players.PlayerRemoving:Connect(function(p)
  39. --Save Data--
  40. local DataStore = game:GetService("DataStoreService"):GetDataStore(p.userId)
  41. local Data = game.ReplicatedStorage.DataHolder[p.UserId].Data:GetChildren()
  42. local s1 = {}
  43. for i=1,#Data do
  44. local n = Data[i].Name
  45. s1[n] = Data[i].Value
  46. end
  47.  
  48. local success, err = pcall(function()
  49. DataStore:SetAsync("Data", s1)
  50. end)
  51.  
  52. if success then
  53. print("Success!")
  54. end
  55.  
  56. -- DataStore:SetAsync("Data",s1)
  57. game.ReplicatedStorage.DataHolder[p.UserId]:Destroy()
  58. print("Saved "..p.Name)
  59. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement