Guest User

Untitled

a guest
Jul 27th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. local Datastore = game:GetService("DataStoreService")
  2. local playerSaveData = game.ServerStorage:WaitForChild('PlayerSaveData')
  3.  
  4. game.Players.PlayerAdded:connect(function(player)
  5. print("PlayerAdded")
  6. local playerFolder = game.ReplicatedStorage.playerFolder:Clone()
  7. playerFolder.Name = player.Name
  8. playerFolder.Parent = game.ServerStorage.PlayerSaveData
  9.  
  10. local c = Instance.new("BoolValue")
  11. c.Name = "CanAutoLoad"
  12. c.Parent = player
  13. c.Value = false
  14.  
  15. local canSave = Instance.new("BoolValue")
  16. canSave.Name = 'canSave'
  17. canSave.Value = true
  18. canSave.Parent = player
  19.  
  20. local key = "user " .. player.userId
  21.  
  22. local playerData = {
  23. customCharacter = '';
  24. equipped = {};
  25. slots = {
  26.  
  27. };
  28. stats = {
  29. food = nil;
  30. health = nil;
  31. melee = nil;
  32. movementSpeed = nil;
  33. stamina = nil;
  34. slotAmnt = nil;
  35. }
  36. }
  37.  
  38. local dataFetchSuccess,ErrorMessage = pcall(function()
  39. playerData = Datastore:GetAsync(key)
  40. end)
  41.  
  42. if dataFetchSuccess then --if success
  43. if playerData ~= nil then -- played before
  44. print("Player Has Played Before")
  45. playerFolder.CustomCharacter.SkinTone.Value = playerData.customCharacter
  46. playerFolder.Stats.Food.Value = playerData.stats.food
  47. playerFolder.Stats.Health.Value = playerData.stats.health
  48. playerFolder.Stats.Melee.Value = playerData.stats.melee
  49. playerFolder.Stats.MovementSpeed.Value = playerData.stats.movementSpeed
  50. playerFolder.Stats.Stamina.Value = playerData.stats.stamina
  51. playerFolder.Stats.SlotAmnt.Value = playerData.stats.slotAmnt
  52. for i = 1,#playerData.stats.slotAmnt do -- for each slots
  53. --Clone stat from storage to players slots folder and fill in data based on PlayerData
  54. print("Creating slot"..i)
  55. local slot = game:GetService("ReplicatedStorage").slot:Clone()
  56. slot.Name = "Slot"..i
  57. slot.ItemAmount.Value = playerData.slots["Slot"..i].slotName
  58. slot.ItemCap.Value = playerData.slots["Slot"..i].itemCap
  59. slot.SlotFull.Value = playerData.slots["Slot"..i].slotFull
  60. slot.Parent = playerFolder.Slots
  61. end
  62. else -- first time playing
  63. print("First Time Playing")
  64. playerFolder.CustomCharacter.SkinTone.Value = "Light Orange";
  65. playerFolder.Stats.Food.Value = 100;
  66. playerFolder.Stats.Health.Value = 100;
  67. playerFolder.Stats.Melee.Value = 1;
  68. playerFolder.Stats.MovementSpeed.Value = 1;
  69. playerFolder.Stats.Stamina.Value = 100;
  70. playerFolder.Stats.SlotAmnt.Value = 0;
  71.  
  72. end
  73. else
  74. print("Couldn't retrieve data")
  75. canSave.Value = false
  76. player:Kick('Could not retrieve data.')
  77. end
  78. end)
  79. game.Players.PlayerRemoving:connect(function(Player)
  80. local Key = "user " .. Player.userId
  81. local b = game.ServerStorage.PlayerSaveData:FindFirstChild(Player.Name):GetChildren()
  82. if Player.canSave.Value==false then return end --Player data can't be saved, so do nothing.
  83.  
  84.  
  85. local PlayerData = {
  86. customCharacter = '';
  87. equipped = {};
  88. slots = {
  89.  
  90. };
  91.  
  92. stats = {
  93. food = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Food.Value;
  94. health = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Health.Value;
  95. melee = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Melee.Value;
  96. movementSpeed = game.ServerStorage.PlayerSaveData[Player.Name].Stats.MovementSpeed.Value;
  97. stamina = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Stamina.Value;
  98. slotAmnt = tonumber(#b);
  99. };
  100. }
  101.  
  102. for i =1,#b do
  103. print("Saving")
  104. PlayerData.slots["Slot"..i] = {
  105. slotName = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).Value;
  106. itemCap = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).ItemCap.Value;
  107. slotFull = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).SlotFull.Value;
  108. };
  109. end
  110.  
  111. local DataWriteSuccess,ErrorMessage=pcall(function() --Once again, we are safely calling a web request. If it fails, we can safely handle it.
  112. Datastore:SetAsync(Key,PlayerData)
  113. end)
  114.  
  115. if not DataWriteSuccess then
  116. print("Failed To Write")
  117. --Uh oh, player data didnt' save. Handle this error.
  118. --We will attempt to save the player's data 5 more times. If it fails after 5 times,
  119. --we will abort the save and the player's data will not be changed in the datastores.
  120.  
  121. local Retry_Count=0
  122.  
  123. while Retry_Count<6 do
  124. wait(60) --Wait 1 minute between each retry
  125. local Succeded,Error=pcall(function()
  126. Datastore:SetAsync(Key,PlayerData)
  127. end)
  128. if Succeded then break end --HURRAY, DATA SAVED!
  129. Retry_Count=Retry_Count+1
  130. end
  131.  
  132. end
  133.  
  134. end)
  135. game:BindToClose(function()
  136. for _, Player in pairs(game:GetService("Players"):GetPlayers()) do
  137. local Key = "user " .. Player.userId
  138. local b = game.ServerStorage.PlayerSaveData:FindFirstChild(Player.Name):GetChildren()
  139. if Player.canSave.Value==false then return end --Player data can't be saved, so do nothing.
  140.  
  141.  
  142. local PlayerData = {
  143. customCharacter = '';
  144. equipped = {};
  145. slots = {
  146.  
  147. };
  148.  
  149. stats = {
  150. food = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Food.Value;
  151. health = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Health.Value;
  152. melee = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Melee.Value;
  153. movementSpeed = game.ServerStorage.PlayerSaveData[Player.Name].Stats.MovementSpeed.Value;
  154. stamina = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Stamina.Value;
  155. slotAmnt = tonumber(#b);
  156. };
  157. }
  158.  
  159. for i =1,#b do
  160. print("Saving")
  161. PlayerData.slots["Slot"..i] = {
  162. slotName = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).Value;
  163. itemCap = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).ItemCap.Value;
  164. slotFull = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).SlotFull.Value;
  165. };
  166. end
  167.  
  168. local DataWriteSuccess,ErrorMessage=pcall(function() --Once again, we are safely calling a web request. If it fails, we can safely handle it.
  169. Datastore:SetAsync(Key,PlayerData)
  170. end)
  171.  
  172. if not DataWriteSuccess then
  173. print("Failed To Write")
  174. --Uh oh, player data didnt' save. Handle this error.
  175. --We will attempt to save the player's data 5 more times. If it fails after 5 times,
  176. --we will abort the save and the player's data will not be changed in the datastores.
  177.  
  178. local Retry_Count=0
  179.  
  180. while Retry_Count<6 do
  181. wait(60) --Wait 1 minute between each retry
  182. local Succeded,Error=pcall(function()
  183. Datastore:SetAsync(Key,PlayerData)
  184. end)
  185. if Succeded then break end --HURRAY, DATA SAVED!
  186. Retry_Count=Retry_Count+1
  187. end
  188.  
  189. end
  190. end
  191. end)
Advertisement
Add Comment
Please, Sign In to add comment