WolfGamesProgrammer

Save Data

Feb 11th, 2024
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.58 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.Ru.Value = value    
  19.                 else if index == 2 then
  20.                         player.leaderstats.Level.Value = value    
  21.                     elseif game.ReplicatedStorage.Backpack:FindFirstChild(value) then
  22.                         local Weapon = game.ReplicatedStorage.Backpack:FindFirstChild(value):Clone()
  23.                         Weapon.Parent = player:WaitForChild("Backpack")
  24.                     end      
  25.                 end
  26.             end
  27.         end
  28.     end
  29.     player.CharacterAdded:Connect(LoadInfo)
  30.  
  31.     function SaveInfo(character)  
  32.         local humanoid = character:FindFirstChildOfClass("Humanoid")
  33.         if humanoid then
  34.             humanoid:UnequipTools()
  35.         end  
  36.  
  37.         local key = player.userId
  38.         local Table = {}
  39.         print("перезапись")
  40.         table.insert(Table,player.leaderstats.Ru.Value)
  41.         table.insert(Table,player.leaderstats.Level.Value)
  42.         for index, value in pairs(player.Backpack:GetChildren()) do
  43.             table.insert(Table,value.Name)
  44.         end
  45.  
  46.         if Table  then
  47.             local success, errorMessage = pcall(function()
  48.                 DS:SetAsync(key, Table)
  49.             end)
  50.             if not success then
  51.                 print("не сработало сохранение")
  52.             end
  53.         end
  54.     end
  55.     player.CharacterRemoving:Connect(SaveInfo)
  56. end)
Advertisement
Add Comment
Please, Sign In to add comment