Advertisement
drakon-firestone

Untitled

Nov 12th, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. local players = game:GetService("Players")
  2.  
  3. local playerData = script.PlayerData
  4.  
  5. local dataStoreService = game:GetService("DataStoreService")
  6. local dataStore = dataStoreService:GetDataStore("Data")
  7. local key = "TestKey2" -- dla łatwego resetu bazy
  8.  
  9.  
  10. local remotes = game.ReplicatedStorage.Remotes
  11. local bank = remotes.Bank
  12. local area = remotes.Area
  13.  
  14.  
  15.  
  16. local function LoadData(player)
  17.  
  18. for i, v in pairs(playerData:GetChildren()) do
  19. v:Clone().Parent = player
  20. end
  21.  
  22. local data
  23.  
  24. local success, errorMsg = pcall(function()
  25.  
  26. data = dataStore:GetAsync(player.UserId .. key)
  27.  
  28. if not data then
  29. return
  30. end
  31.  
  32. for key, folder in pairs(data) do
  33.  
  34. if key == "leaderstats" then
  35. for item, amount in pairs(folder) do
  36. player.leaderstats[item].Value = amount
  37. end
  38. end
  39.  
  40. if key == "Areas" then
  41.  
  42. for item, _ in pairs(folder) do
  43.  
  44. if not player.Areas:FindFirstChild(item) then
  45.  
  46. local value = Instance.new("StringValue", player.Areas)
  47. value.Name = item
  48. area:FireClient(player, workspace.Areas[item].Barrier)
  49.  
  50. end
  51.  
  52. end
  53.  
  54. end
  55.  
  56.  
  57.  
  58. end
  59.  
  60. print("Loaded data")
  61. print(data)
  62.  
  63. end)
  64.  
  65. if not success then
  66. warn(errorMsg)
  67. wait(5)
  68. LoadData(player)
  69. end
  70.  
  71.  
  72.  
  73. end
  74.  
  75.  
  76.  
  77. local function SaveData(player)
  78. local data = {}
  79. local count = 0
  80. local success, errorMsg = pcall(function()
  81. for i, folder in pairs(player:GetChildren()) do
  82. if folder:IsA("Folder") then
  83. data[folder.Name] = {}
  84. for i, item in pairs(folder:GetChildren()) do
  85. data[folder.Name][item.Name] = item.Value
  86. end
  87. end
  88. end
  89. dataStore:SetAsync(player.UserId .. key, data)
  90. end)
  91. if not success then
  92. warn(errorMsg)
  93. wait(5)
  94. SaveData(player)
  95. end
  96. print("Saved data")
  97. print(data)
  98. end
  99.  
  100.  
  101.  
  102.  
  103. players.PlayerRemoving:Connect(SaveData)
  104. players.PlayerAdded:Connect(LoadData)
  105.  
  106.  
  107.  
  108. bank.OnServerEvent:Connect(function(player, action, currency, amount)
  109. if action == "+" then
  110. if currency == "Coins" then
  111. player.leaderstats.Coins.Value += amount
  112. elseif currency == "Diamonds" then
  113. player.leaderstats.Diamonds.Value += amount
  114. end
  115. end
  116. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement