Guest User

Roblox - Saving Script

a guest
Mar 21st, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. local Player = game:GetService("Players")
  2. local DataStoreService = game:GetService("DataStoreService")
  3. local DataStore = DataStoreService:GetDataStore("-xBuckDatax-")
  4. local RunService = game:GetService('RunService')
  5. local plrFolder = game.Workspace.PlayerFolder
  6. local key
  7. local plr
  8. local plrName
  9. local plrCount = 0
  10.  
  11. -- Making the saving inside a function it shortened your code by a bit.
  12. local function Save(player)
  13. if plrCount == 0 then
  14. print('BindToClose')
  15. game:BindToClose(Save)
  16. return
  17. end
  18. print(player)
  19. key = game.Workspace.PlayerFolder:WaitForChild(player.Name).Value
  20. plr = game.Workspace:WaitForChild(player.Name)
  21. local save = {
  22. ["SeerBux"] = player.leaderstats["§ SeerBux"].Value,
  23. ["SpdWaitTime"] = player.WaitTime.WaitTimeSpd.Value,
  24. ["StrWaitTime"] = player.WaitTime.WaitTimeStr.Value,
  25. ["walkSpeed"] = plr.Humaoid.WalkSspeed,
  26. ["customize"] = player.Customize.Value,
  27. ["Exp"] = player.leaderstats["ExperiencePts"].Value,
  28. ["level"] = player.leaderstats["Level"].Value,
  29. ["SpdCheck"] = player.SpdCheck.Value,
  30. ['energy'] = player.EnergyCount.Value,
  31. ['enbool'] = player.EnergyBool.Value
  32. }
  33.  
  34. --saves their key and money to the data store
  35. local success, err = pcall(function()
  36. DataStore:SetAsync(key, save)
  37. end)
  38. if success then
  39. print('Success')
  40. end
  41.  
  42. if not success then
  43. warn("Failed to over-write data"..tostring(err))
  44. print("Failed to over-write data"..tostring(err))
  45. return
  46. end
  47. print('Successful Save')
  48. end
  49.  
  50. -- This function loads the player's data
  51. local function Load(player)
  52. -- saves the player Id as key
  53. wait(1)
  54. local key = player.UserId
  55. local plr = workspace:WaitForChild(player.Name)
  56.  
  57. local moneyAlready
  58.  
  59. local success, err = pcall(function()
  60. moneyAlready = DataStore:GetAsync(key)
  61. end)
  62.  
  63. if not success then
  64. warn("Failed to read data"..tostring(err))
  65. print("Failed to read data"..tostring(err))
  66. return
  67. end
  68.  
  69. if moneyAlready then
  70. player.leaderstats['§ SeerBux'].Value = moneyAlready.SeerBux
  71. player.WaitTime.WaitTimeSpd.Value = moneyAlready.SpdWaitTime
  72. player.WaitTime.WaitTimeStr.Value = moneyAlready.StrWaitTime
  73. plr:WaitForChild('Humanoid').WalkSpeed = moneyAlready.walkSpeed
  74. player.Customize.Value = moneyAlready.customize
  75. player.leaderstats.ExperiencePts.Value = moneyAlready.ExperiencePts
  76. player.leaderstats.Level.Value = moneyAlready.Level
  77. player.SpdCheck.Value = moneyAlready.SpdCheck
  78. player.EnergyCount.Value = moneyAlready.energy
  79. player.EnergyBool.Value = moneyAlready.enbool
  80.  
  81. else
  82. Save(player)
  83. end
  84. end
  85.  
  86. game.Players.PlayerRemoving:Connect(function()
  87. plrCount = plrCount - 1
  88. end)
  89. game.Players.PlayerAdded:Connect(function()
  90. plrCount = plrCount + 1
  91. end)
  92.  
  93. game.Players.PlayerAdded:Connect(Load)
  94. game.Players.PlayerRemoving:Connect(Save)
Advertisement
Add Comment
Please, Sign In to add comment