Advertisement
SxScripting

Datastore Script

Feb 26th, 2021
1,914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. local SaveData = game:GetService("DataStoreService"):GetDataStore("Lives")
  2.  
  3. game.Players.PlayerAdded:Connect(function(Player)
  4.  
  5. local Character = Player.Character or Player.CharacterAdded:Wait()
  6. local Humanoid = Character.Humanoid
  7.  
  8. local SystemFolder = Instance.new("Folder",Player)
  9. SystemFolder.Name = "SystemFolder"
  10.  
  11. local Lives = Instance.new("IntValue",SystemFolder)
  12. Lives.Value = 3
  13. Lives.Name = "Lives"
  14.  
  15. local Times = Instance.new("IntValue", SystemFolder)
  16. Times.Value = os.time()
  17. Times.Name = "Times"
  18.  
  19. local Success, Value = pcall(function()
  20. return SaveData:GetAsync(Player.UserId)
  21. end)
  22.  
  23. local function LivesChanging()
  24. if Lives.Value == 0 then Player.Character.HumanoidRootPart.CFrame = workspace.NoLifePart.CFrame end
  25.  
  26. if Lives.Value < 0 then
  27. Lives.Value = 0
  28. elseif Lives.Value > 15 then
  29. Lives.Value = 15
  30. end
  31.  
  32. end
  33.  
  34. local function CheckTime()
  35. Times.Value = Times.Value + 1
  36. if Times.Value >= 3600 then
  37. Times.Value = 0
  38. Lives.Value = Lives.Value + 1
  39. end
  40. end
  41.  
  42. LivesChanging()
  43. Lives.Changed:Connect(LivesChanging)
  44.  
  45. if Success then
  46. if Value then
  47. Lives.Value = Value[1]
  48. Times.Value = Value[2]
  49. else
  50. Times.Value = 0
  51. Lives.Value = 3
  52. end
  53. end
  54. spawn(function()
  55. while wait(1) do
  56. CheckTime()
  57. end
  58. end)
  59. end)
  60.  
  61. game.Players.PlayerRemoving:Connect(function(Player)
  62.  
  63. local StatTable = {
  64. Player:WaitForChild("SystemFolder").Lives.Value;
  65. Player:WaitForChild("SystemFolder").Times.Value;
  66. }
  67.  
  68. local Success, Error = pcall(function()
  69. return SaveData:SetAsync(Player.UserId, StatTable)
  70. end)
  71.  
  72. if Error then
  73. print(Error)
  74. end
  75.  
  76. end)
  77.  
  78.  
  79. game:BindToClose(function()
  80. for i,players in pairs(game.Players:GetPlayers()) do
  81.  
  82. local StatTable = {
  83. players:WaitForChild("SystemFolder").Lives.Value;
  84. players:WaitForChild("SystemFolder").Times.Value;
  85. }
  86.  
  87. local Success, Error = pcall(function()
  88. return SaveData:SetAsync(players.UserId, StatTable)
  89. end)
  90.  
  91. if Error then
  92. print(Error)
  93. end
  94.  
  95. end
  96. end)
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement