Advertisement
drakon-firestone

Untitled

Nov 5th, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 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 = "TestKey1" -- 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. for key, folder in pairs(data) do
  29.  
  30. if key == "leaderstats" then
  31. for item, amount in pairs(folder) do
  32. player.leaderstats[item].Value = amount
  33. end
  34. end
  35.  
  36. if key == "Areas" then
  37.  
  38. for item, _ in pairs(folder) do
  39.  
  40. if not player.Areas:FindFirstChild(item) then
  41.  
  42. local value = Instance.new("StringValue", player.Areas)
  43. value.Name = item
  44. area:FireClient(player, workspace.Areas[item].Barrier)
  45.  
  46. end
  47.  
  48. end
  49.  
  50. end
  51.  
  52.  
  53.  
  54. end
  55.  
  56. print("Loaded data")
  57. print(data)
  58.  
  59. end)
  60.  
  61. if not success then
  62. warn(errorMsg)
  63. wait(5)
  64. LoadData(player)
  65. end
  66.  
  67.  
  68.  
  69. end
  70.  
  71.  
  72.  
  73. local function SaveData(player)
  74. local data = {}
  75. local count = 0
  76. local success, errorMsg = pcall(function()
  77. for i, folder in pairs(player:GetChildren()) do
  78. data[folder.Name] = {}
  79. for i, item in pairs(folder:GetChildren()) do
  80. data[folder.Name][item.Name] = item.Value
  81. end
  82. end
  83. dataStore:SetAsync(player.UserId .. key, data)
  84. end)
  85. if not success then
  86. warn(errorMsg)
  87. wait(5)
  88. SaveData(player)
  89. end
  90. print("Saved data")
  91. print(data)
  92. end
  93.  
  94.  
  95.  
  96.  
  97. players.PlayerRemoving:Connect(SaveData)
  98. players.PlayerAdded:Connect(LoadData)
  99.  
  100.  
  101.  
  102. bank.OnServerEvent:Connect(function(player, action, currency, amount)
  103. if action == "+" then
  104. if currency == "Coins" then
  105. player.leaderstats.Coins.Value += amount
  106. elseif currency == "Diamonds" then
  107. player.leaderstats.Diamonds.Value += amount
  108. end
  109. end
  110. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement