NeonStranger

Inventory Saving

Dec 8th, 2021 (edited)
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.09 KB | None | 0 0
  1. local ds2 = require(game:GetService("ServerScriptService").Modules.DataStore2)
  2. local itemModule = require(game:GetService("ServerScriptService").Modules.ItemModule)
  3. local toolsFolder = game.ReplicatedFirst.Tools
  4. local resourceFolder = game.ReplicatedFirst.Resources
  5. ds2.Combine("DATA", "Tools")
  6. --Saving System--
  7.  
  8. game.Players.PlayerAdded:Connect(function(player)
  9.    
  10.     local Backpack = player.Backpack
  11.     local Inventory = player.PlayerGui:WaitForChild("Inventory")
  12.     local toolStore = ds2("tools_", player)
  13.     local resourceStore = ds2("resources_", player)
  14.     local tools = toolStore:Get()
  15.     local resources = resourceStore:Get()
  16.    
  17.     print("Retrieving Saved Tools")
  18.     if tools ~= nil then
  19.         for i, toolSaved in pairs(tools) do
  20.             if toolSaved ~= nil then
  21.                 itemModule.addItemtoInventory(player, toolSaved, "Tool", 1)        
  22.             end
  23.         end
  24.        
  25.         for i, r in pairs(resources) do
  26.             if r ~= nil then
  27.  
  28.                 local resource = resourceFolder[tostring(r[1])]:Clone()
  29.                 resource.Info.Type.Value = r[2]
  30.                 resource.Info.Distance.Value = r[3]
  31.                 resource.Info.FireRate.Value = r[4]
  32.                 resource.Info.Image.Value = r[5]
  33.                 resource.Parent = player.Backpack
  34.                 itemModule.addItemtoInventory(player, resource, "Tool", 1)     
  35.                
  36.             end
  37.         end
  38.     end
  39.    
  40.    
  41.    
  42.    
  43.    
  44.    
  45.    
  46.    
  47.     player.CharacterRemoving:Connect(function(character)
  48.        
  49.         character.Humanoid:UnequipTools()
  50.        
  51.     end)
  52. end)
  53.  
  54. game.Players.PlayerRemoving:Connect(function(player)
  55.    
  56.     local toolStore = ds2("Tools", player)
  57.     local toolsOwned = {}
  58.    
  59.     for i, tool in pairs(player.Backpack:GetChildren()) do
  60.         local toolInfo = {}
  61.         table.insert(toolInfo, tool.Name)
  62.         for _, child in pairs(tool.Info:GetChildren()) do
  63.             table.insert(toolInfo, child.Value)
  64.         end
  65.         table.insert(toolsOwned, toolInfo)
  66.         print("saving " .. tool.Name)
  67.     end
  68.    
  69. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  70.     local success, errormessage = pcall(function()
  71.         toolStore:Set(toolsOwned)
  72.     end)
  73.     if success then print(player.Name .. "'s Inventory Data Saved") end
  74.     if errormessage then warn(errormessage) end
  75.    
  76. end)
  77.  
  78. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  79.  
Add Comment
Please, Sign In to add comment