WolfGamesProgrammer

Save Data for leaderstats

Apr 15th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.47 KB | Source Code | 0 0
  1. local DS = game:GetService("DataStoreService"):GetDataStore("Backpack")
  2.  
  3. game.Players.PlayerAdded:connect(function(player)
  4.  
  5.     function LoadInfo(character)
  6.         wait(1)
  7.         local key = player.userId
  8.         local success, currentExperience = pcall(function()
  9.             return DS:GetAsync(key)
  10.         end)
  11.         if not success then
  12.             print("не сработала загрузка")
  13.         end
  14.  
  15.         if currentExperience then
  16.             for index, value in pairs(currentExperience) do
  17.                 if index == 1 then
  18.                     player.leaderstats.Money.Value = value    
  19.                  
  20.                     elseif game.ReplicatedStorage.Backpack:FindFirstChild(value) then
  21.                         local Weapon = game.ReplicatedStorage.Backpack:FindFirstChild(value):Clone()
  22.                         Weapon.Parent = player:WaitForChild("Backpack")
  23.                          
  24.                 end
  25.             end
  26.         end
  27.     end
  28.     player.CharacterAdded:Connect(LoadInfo)
  29.  
  30.     function SaveInfo(character)  
  31.         local humanoid = character:FindFirstChildOfClass("Humanoid")
  32.         if humanoid then
  33.             humanoid:UnequipTools()
  34.         end  
  35.  
  36.         local key = player.userId
  37.         local Table = {}
  38.         print("перезапись")
  39.         table.insert(Table,player.leaderstats.Money.Value)
  40.        
  41.         for index, value in pairs(player.Backpack:GetChildren()) do
  42.             table.insert(Table,value.Name)
  43.         end
  44.  
  45.         if Table  then
  46.             local success, errorMessage = pcall(function()
  47.                 DS:SetAsync(key, Table)
  48.             end)
  49.             if not success then
  50.                 print("не сработало сохранение")
  51.             end
  52.         end
  53.     end
  54.     player.CharacterRemoving:Connect(SaveInfo)
  55. end)
Advertisement
Add Comment
Please, Sign In to add comment