Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. local datastore = game:GetService("DataStoreService"):GetDataStore("p0vdStore")
  2.  
  3. --this is just for testing
  4. function printTable(myTable)
  5. for i, v in pairs(myTable)do
  6. print(i,v)
  7. end
  8. end
  9. game.Players.PlayerAdded:Connect(function(player)
  10. -- datastore:RemoveAsync("user-"..player.userId)
  11.  
  12. --create a new folder that stores the players data
  13. --and put that folder into the playerInformation folder
  14. local dataFolder = game.ReplicatedStorage.Data:Clone()
  15. dataFolder.Name = player.Name
  16. dataFolder.Parent = game.ReplicatedStorage.PlayerInformation
  17.  
  18.  
  19. --try to get data from datastore
  20. local success, data = pcall(function()
  21. return datastore:GetAsync("user-"..player.userId)
  22. end)
  23.  
  24. if(success)then --if we got it without a problem load data into game
  25. if data then --and we have saved data from the datastore
  26. print("loaded items from datastore: ")
  27. printTable(data)
  28.  
  29. --set values in folder to the values that are in the datastore
  30. dataFolder.Coins.Value = data.Coins
  31. dataFolder.IsNewUser.Value = data.IsNewUser
  32. dataFolder.UsedCode.Value = data.UsedCode
  33. dataFolder.ClawboxTexture.Value = data.ClawboxTexture
  34. dataFolder.PayoutRate.Value = data.PayoutRate
  35.  
  36. --set the bools for the items that we own in the closet to true
  37. for _, itemName in pairs(data.Closet)do
  38. local bool = dataFolder.Closet:FindFirstChild(itemName)
  39. bool.Value = true
  40. end
  41. else
  42. print("you are a new player so we don't need anything fron datastore")
  43. --do nothing, we already loaded the default data when we cloned the data folder from the replicated storage
  44. end
  45. else
  46. print("we couldnt get the data")
  47. end
  48. end)
  49.  
  50. game.Players.PlayerRemoving:connect(function(player)
  51. local dataFolder = game.ReplicatedStorage.PlayerInformation[player.Name]
  52.  
  53. local toSave = {
  54. Coins = dataFolder.Coins.Value,
  55. PayoutRate = dataFolder.PayoutRate.Value,
  56. IsNewUser = dataFolder.IsNewUser.Value,
  57. ClawboxTexture = dataFolder.ClawboxTexture.Value,
  58. UsedCode = dataFolder.UsedCode.Value,
  59. }
  60.  
  61. --save all the owned items into a table and then save that table into the toSave table
  62. local closet = {}
  63. for i, v in pairs(dataFolder.Closet:GetChildren())do
  64. if(v.Value == true)then
  65. table.insert(closet, v.Name)
  66. end
  67. end
  68.  
  69. toSave["Closet"] = closet
  70.  
  71. --this is for debugging
  72. print("saving the following to datastore: ")
  73. printTable(toSave)
  74.  
  75. --attempt to save to datastore
  76. local success, err = pcall(function()
  77. datastore:SetAsync("user-" .. player.userId, toSave)
  78. end)
  79.  
  80. dataFolder:Destroy() --remove the folder since we don't need it anymore
  81. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement