Advertisement
drakon-firestone

Untitled

Nov 10th, 2023 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. local players = game:GetService("Players")
  2.  
  3. local playerData = script.PlayerData
  4.  
  5. local dataStoreService = game:GetService("DataStoreService")
  6. -- tworzenie/odczytywanie bazy danych o nazwie "Data"
  7. local dataStore = dataStoreService:GetDataStore("Data")
  8. -- do szybkiego resetu bazy
  9. local key = "TestKey1"
  10.  
  11. local remotes = game.ReplicatedStorage.Remotes
  12. local bank = remotes.Bank
  13. local area = remotes.Area
  14.  
  15.  
  16.  
  17. local function LoadData(player)
  18. for i, folder in pairs(playerData:GetChildren()) do
  19. folder: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. if key == "leaderstats" then
  30. for item, amount in pairs(folder) do
  31. player.leaderstats[item].Value = amount
  32. end
  33. end
  34.  
  35. if key == "Areas" then
  36. for item, _ in pairs(folder) do
  37. if not player.Areas:FindFirstChild(item) then
  38. local value = Instance.new("StringValue", player.Areas)
  39. value.Name = item
  40. area:FireClient(player, workspace.Areas[item].Barrier)
  41. end
  42. end
  43. end
  44. end
  45.  
  46. print("Loaded data")
  47. print(data)
  48.  
  49.  
  50. end)
  51.  
  52. if not success then
  53. warn(errorMsg)
  54. wait(5)
  55. LoadData(player)
  56. end
  57.  
  58.  
  59. end
  60.  
  61.  
  62.  
  63. local function SaveData(player)
  64. local data = {}
  65. local count = 0
  66. local success, errorMsg = pcall(function()
  67. for i, folder in pairs(player:GetChildren()) do
  68. data[folder.Name] = {}
  69. for i, item in pairs(folder:GetChildren()) do
  70. data[folder.Name][item.Name] = item.Value
  71. end
  72. end
  73. dataStore:SetAsync(player.UserId .. key, data)
  74. end)
  75. if not success then
  76. warn(errorMsg)
  77. wait(5)
  78. SaveData(player)
  79. end
  80. print("Saved data")
  81. print(data)
  82. end
  83.  
  84.  
  85.  
  86. players.PlayerAdded:Connect(LoadData)
  87. players.PlayerRemoving:Connect(SaveData)
  88.  
  89.  
  90. bank.OnServerEvent:Connect(function(player, action, currency, amount)
  91. if action == "+" then
  92. player.leaderstats[currency].Value += amount
  93. end
  94. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement