Advertisement
drakon-firestone

Untitled

Nov 19th, 2023 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 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 = "TestKey3" -- 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. if key == "Pets" then
  59. for id, petName in pairs(folder) do
  60. local pet = Instance.new("StringValue", player.Pets)
  61. pet.Name = petName
  62. pet.Value = id
  63. remotes:WaitForChild("CreateTemplate"):FireClient(player, petName, id)
  64. end
  65. end
  66.  
  67.  
  68.  
  69.  
  70.  
  71. end
  72.  
  73. print("Loaded data")
  74. print(data)
  75.  
  76. end)
  77.  
  78. if not success then
  79. warn(errorMsg)
  80. wait(5)
  81. LoadData(player)
  82. end
  83.  
  84. end
  85.  
  86.  
  87.  
  88. local function SaveData(player)
  89. local data = {}
  90. local count = 0
  91. local success, errorMsg = pcall(function()
  92. for i, folder in pairs(player:GetChildren()) do
  93. if folder:IsA("Folder") then
  94. data[folder.Name] = {}
  95. for i, item in pairs(folder:GetChildren()) do
  96.  
  97. if folder.Name == "Pets" then
  98. data[folder.Name][item.Value] = item.Name
  99. else
  100. data[folder.Name][item.Name] = item.Value
  101. end
  102.  
  103.  
  104. end
  105. end
  106. end
  107. dataStore:SetAsync(player.UserId .. key, data)
  108. end)
  109. if not success then
  110. warn(errorMsg)
  111. wait(5)
  112. SaveData(player)
  113. end
  114. print("Saved data")
  115. print(data)
  116. end
  117.  
  118.  
  119.  
  120.  
  121. players.PlayerRemoving:Connect(SaveData)
  122. players.PlayerAdded:Connect(LoadData)
  123.  
  124.  
  125.  
  126. bank.OnServerEvent:Connect(function(player, action, currency, amount)
  127. if action == "+" then
  128. if currency == "Coins" then
  129. player.leaderstats.Coins.Value += amount
  130. elseif currency == "Diamonds" then
  131. player.leaderstats.Diamonds.Value += amount
  132. end
  133. end
  134. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement