Advertisement
drakon-firestone

Untitled

Jan 5th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 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 = "TestKey6"
  10.  
  11. local remotes = game.ReplicatedStorage.Remotes
  12. local bank = remotes.Bank
  13. local area = remotes.Area
  14. local createTemplate = remotes.CreateTemplate
  15.  
  16.  
  17.  
  18. local function LoadData(player)
  19. for i, folder in pairs(playerData:GetChildren()) do
  20. folder:Clone().Parent = player
  21. end
  22.  
  23. local data
  24.  
  25. local success, errorMsg = pcall(function()
  26.  
  27. data = dataStore:GetAsync(player.UserId .. key)
  28.  
  29. if not data then
  30. return
  31. end
  32.  
  33. for key, folder in pairs(data) do
  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. for item, _ in pairs(folder) do
  42. if not player.Areas:FindFirstChild(item) then
  43. local value = Instance.new("StringValue", player.Areas)
  44. value.Name = item
  45. area:FireClient(player, workspace.Areas[item].Barrier)
  46. end
  47. end
  48. end
  49.  
  50. if key == "Pets" then
  51. for id, petName in pairs(folder) do
  52. local pet = Instance.new("StringValue", player.Pets)
  53. pet.Name = petName
  54. pet.Value = id
  55. createTemplate:FireClient(player, petName, id)
  56. end
  57. end
  58.  
  59. if key == "Codes" then
  60. for code, value in pairs(folder) do
  61. local usedCode = Instance.new("StringValue", player.Codes)
  62. usedCode.Name = code
  63. end
  64. end
  65.  
  66. end
  67.  
  68. print("Loaded data")
  69. print(data)
  70.  
  71.  
  72. end)
  73.  
  74. if not success then
  75. warn(errorMsg)
  76. wait(5)
  77. LoadData(player)
  78. end
  79.  
  80.  
  81. end
  82.  
  83.  
  84.  
  85. local function SaveData(player)
  86. local data = {}
  87. local count = 0
  88. local success, errorMsg = pcall(function()
  89. for i, folder in pairs(player:GetChildren()) do
  90. if folder:IsA("Folder") then
  91. data[folder.Name] = {}
  92. for i, item in pairs(folder:GetChildren()) do
  93.  
  94. if folder.Name == "Pets" then
  95. data[folder.Name][item.Value] = item.Name
  96. else
  97. data[folder.Name][item.Name] = item.Value
  98. end
  99.  
  100.  
  101. end
  102. end
  103.  
  104.  
  105. end
  106. dataStore:SetAsync(player.UserId .. key, data)
  107. end)
  108. if not success then
  109. warn(errorMsg)
  110. wait(5)
  111. SaveData(player)
  112. end
  113. print("Saved data")
  114. print(data)
  115. end
  116.  
  117.  
  118.  
  119. players.PlayerAdded:Connect(LoadData)
  120. players.PlayerRemoving:Connect(SaveData)
  121.  
  122.  
  123. bank.OnServerEvent:Connect(function(player, action, currency, amount)
  124. if action == "+" then
  125. player.leaderstats[currency].Value += amount
  126. end
  127. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement