Advertisement
HowToRoblox

InventoryServer

Jan 8th, 2023
2,827
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.99 KB | None | 1 0
  1. local hotbarSlots = 9
  2.  
  3.  
  4. local rs = game:GetService("ReplicatedStorage"):WaitForChild("InventoryReplicatedStorage")
  5. local remotes = rs:WaitForChild("RemoteEvents")
  6.  
  7.  
  8. game.Players.PlayerAdded:Connect(function(plr:Player)
  9.  
  10.     local inventory = Instance.new("Folder")
  11.     inventory.Name = "Inventory"
  12.     inventory.Parent = plr
  13.    
  14.     local hotbar = Instance.new("Folder")
  15.     hotbar.Name = "Hotbar"
  16.     hotbar.Parent = plr
  17.    
  18.     for slotNum = 1, hotbarSlots do
  19.         local newSlot = Instance.new("ObjectValue")
  20.         newSlot.Name = slotNum
  21.        
  22.         newSlot.Parent = hotbar
  23.     end
  24.    
  25.     local equipped = Instance.new("ObjectValue")
  26.     equipped.Name = "Equipped"
  27.     equipped.Parent = plr
  28.    
  29.    
  30.     plr.CharacterAdded:Connect(function(char)
  31.        
  32.         local backpack = plr.Backpack
  33.        
  34.         for slotNum, tool in pairs(backpack:GetChildren()) do
  35.             if slotNum <= hotbarSlots then
  36.                 hotbar[slotNum].Value = tool
  37.             else
  38.                 break
  39.             end
  40.         end
  41.        
  42.         backpack.ChildAdded:Connect(function(child)
  43.            
  44.             for _, slot in pairs(hotbar:GetChildren()) do
  45.                 if slot.Value == child then return end
  46.             end
  47.            
  48.             if child:IsA("Tool") then
  49.                
  50.                 if #backpack:GetChildren() <= hotbarSlots then
  51.                     hotbar[#backpack:GetChildren()].Value = child
  52.                    
  53.                 else
  54.                     local newItemValue = Instance.new("ObjectValue")
  55.                     newItemValue.Value = child
  56.                     newItemValue.Name = child.Name
  57.                     newItemValue.Parent = inventory
  58.                 end
  59.             end
  60.         end)
  61.        
  62.         char.ChildAdded:Connect(function(child)
  63.            
  64.             if child:IsA("Tool") and equipped.Value ~= child then
  65.                
  66.                 for _, slot in pairs(hotbar:GetChildren()) do
  67.                     if slot.Value == child then return end
  68.                 end
  69.                
  70.                 local firstFreeSlot = nil
  71.                 for slotNum, slot in pairs(hotbar:GetChildren()) do
  72.  
  73.                     if not slot.Value then
  74.                         firstFreeSlot = slot
  75.                         break
  76.  
  77.                     elseif slotNum == hotbarSlots then
  78.  
  79.                         for slotNumber, slotValue in pairs(hotbar:GetChildren()) do
  80.  
  81.                             if slotNumber > 1 then
  82.                                 hotbar[slotNumber - 1].Value = slotValue.Value
  83.                             else
  84.                                 local newItemValue = Instance.new("ObjectValue")
  85.                                 newItemValue.Value = slotValue.Value
  86.                                 newItemValue.Name = slotValue.Value.Name
  87.                                 newItemValue.Parent = inventory
  88.                             end
  89.                         end
  90.  
  91.                         firstFreeSlot = hotbar[hotbarSlots]
  92.                     end
  93.                 end
  94.                
  95.                 firstFreeSlot.Value = child
  96.                
  97.                 equipped.Value = child
  98.                
  99.                 child.Unequipped:Wait()
  100.  
  101.                 if not plr.Character:FindFirstChildOfClass("Tool") then
  102.                     equipped.Value = nil
  103.                 end
  104.  
  105.                 if child.Parent ~= plr.Backpack then
  106.  
  107.                     for slotNum, slot in pairs(hotbar:GetChildren()) do
  108.                         if slot.Value == child then
  109.  
  110.                             local occupiedSlots = 0
  111.                             for slotNumber, slotValue in pairs(hotbar:GetChildren()) do
  112.                                 if slotValue.Value then
  113.                                     occupiedSlots += 1
  114.                                 end
  115.                             end
  116.  
  117.                             slot.Value = nil
  118.  
  119.                             if slotNum < occupiedSlots then
  120.  
  121.                                 for slotNumber, slotValue in pairs(hotbar:GetChildren()) do
  122.                                     if slotNumber > slotNum and slotValue.Value then
  123.                                         hotbar[slotNumber - 1].Value = slotValue.Value
  124.                                         slotValue.Value = nil
  125.                                     end
  126.                                 end
  127.                             end
  128.  
  129.                             break
  130.                         end
  131.                     end
  132.                 end
  133.             end
  134.         end)
  135.     end)
  136. end)
  137.  
  138.  
  139. remotes.Equip.OnServerEvent:Connect(function(plr:Player, toolToEquip:Tool)
  140.    
  141.     if not plr.Character or not plr.Character:FindFirstChild("Humanoid") or plr.Character.Humanoid.Health == 0 then return end
  142.    
  143.     local hotbar = plr.Hotbar
  144.     local equippedVal = plr.Equipped
  145.    
  146.     if toolToEquip and toolToEquip:IsA("Tool") then
  147.        
  148.         if toolToEquip.Parent == plr.Backpack then
  149.  
  150.             if equippedVal.Value then
  151.                 equippedVal.Value.Parent = plr.Backpack
  152.             end
  153.            
  154.             equippedVal.Value = toolToEquip
  155.             toolToEquip.Parent = plr.Character
  156.            
  157.         elseif toolToEquip.Parent == plr.Character then
  158.            
  159.             equippedVal.Value.Parent = plr.Backpack
  160.             equippedVal.Value = nil
  161.         end
  162.        
  163.         toolToEquip.Unequipped:Wait()
  164.        
  165.         if not plr.Character:FindFirstChildOfClass("Tool") then
  166.             equippedVal.Value = nil
  167.         end
  168.        
  169.         if toolToEquip.Parent ~= plr.Backpack then
  170.            
  171.             for slotNum, slot in pairs(hotbar:GetChildren()) do
  172.                 if slot.Value == toolToEquip then
  173.  
  174.                     local occupiedSlots = 0
  175.                     for slotNumber, slotValue in pairs(hotbar:GetChildren()) do
  176.                         if slotValue.Value then
  177.                             occupiedSlots += 1
  178.                         end
  179.                     end
  180.  
  181.                     slot.Value = nil
  182.  
  183.                     if slotNum < occupiedSlots then
  184.  
  185.                         for slotNumber, slotValue in pairs(hotbar:GetChildren()) do
  186.                             if slotNumber > slotNum and slotValue.Value then
  187.                                 hotbar[slotNumber - 1].Value = slotValue.Value
  188.                                 slotValue.Value = nil
  189.                             end
  190.                         end
  191.                     end
  192.  
  193.                     break
  194.                 end
  195.             end
  196.         end
  197.        
  198.     elseif not toolToEquip then
  199.        
  200.         if equippedVal.Value then
  201.             equippedVal.Value.Parent = plr.Backpack
  202.             equippedVal.Value = nil
  203.         end
  204.     end
  205. end)
  206.  
  207. remotes.Drop.OnServerEvent:Connect(function(plr:Player, toolToDrop:Tool)
  208.  
  209.     if not plr.Character or not plr.Character:FindFirstChild("Humanoid") or plr.Character.Humanoid.Health == 0 then return end
  210.  
  211.     local backpack = plr.Backpack
  212.     local inventory = plr.Inventory
  213.    
  214.     if toolToDrop and toolToDrop:IsA("Tool") and toolToDrop.Parent == backpack then
  215.        
  216.         for _, toolVal in pairs(inventory:GetChildren()) do
  217.             if toolVal.Value == toolToDrop then
  218.                 toolVal:Destroy()
  219.                 break
  220.             end
  221.         end
  222.        
  223.         toolToDrop.Parent = workspace
  224.         toolToDrop.Handle.CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.LookVector * 6
  225.     end
  226. end)
  227.  
  228.  
  229. remotes.ToHotbar.OnServerEvent:Connect(function(plr:Player, toolToHotbar:Tool)
  230.    
  231.     if not plr.Character or not plr.Character:FindFirstChild("Humanoid") or plr.Character.Humanoid.Health == 0 then return end
  232.    
  233.     local backpack = plr.Backpack
  234.     local hotbar = plr.Hotbar
  235.     local inventory = plr.Inventory
  236.    
  237.     if toolToHotbar and toolToHotbar:IsA("Tool") and toolToHotbar.Parent == backpack then
  238.        
  239.         local firstFreeSlot = nil
  240.         for slotNum, slot in pairs(hotbar:GetChildren()) do
  241.            
  242.             if not slot.Value then
  243.                 firstFreeSlot = slot
  244.                 break
  245.                
  246.             elseif slotNum == hotbarSlots then
  247.                
  248.                 for slotNumber, slotValue in pairs(hotbar:GetChildren()) do
  249.                    
  250.                     if slotNumber > 1 then
  251.                         hotbar[slotNumber - 1].Value = slotValue.Value
  252.                     else
  253.                         local newItemValue = Instance.new("ObjectValue")
  254.                         newItemValue.Value = slotValue.Value
  255.                         newItemValue.Name = slotValue.Value.Name
  256.                         newItemValue.Parent = inventory
  257.                     end
  258.                 end
  259.  
  260.                 firstFreeSlot = hotbar[hotbarSlots]
  261.             end
  262.         end
  263.        
  264.         for _, toolVal in pairs(inventory:GetChildren()) do
  265.             if toolVal.Value == toolToHotbar then
  266.                
  267.                 firstFreeSlot.Value = toolVal.Value
  268.                 toolVal:Destroy()
  269.             end
  270.         end
  271.     end
  272. end)
  273.  
  274. remotes.ToInventory.OnServerEvent:Connect(function(plr:Player, toolToInventory:Tool)
  275.    
  276.     if not plr.Character or not plr.Character:FindFirstChild("Humanoid") or plr.Character.Humanoid.Health == 0 then return end
  277.    
  278.     local backpack = plr.Backpack
  279.     local hotbar = plr.Hotbar
  280.     local inventory = plr.Inventory
  281.    
  282.     if toolToInventory and toolToInventory:IsA("Tool") and (toolToInventory.Parent == backpack or toolToInventory.Parent == plr.Character) then
  283.        
  284.         if toolToInventory.Parent == plr.Character then
  285.             toolToInventory.Parent = backpack
  286.         end
  287.        
  288.         for slotNum, slot in pairs(hotbar:GetChildren()) do
  289.             if slot.Value == toolToInventory then
  290.    
  291.                 local occupiedSlots = 0
  292.                 for slotNumber, slotValue in pairs(hotbar:GetChildren()) do
  293.                     if slotValue.Value then
  294.                         occupiedSlots += 1
  295.                     end
  296.                 end
  297.                
  298.                 slot.Value = nil
  299.                
  300.                 if slotNum < occupiedSlots then
  301.                    
  302.                     for slotNumber, slotValue in pairs(hotbar:GetChildren()) do
  303.                         if slotNumber > slotNum and slotValue.Value then
  304.                             hotbar[slotNumber - 1].Value = slotValue.Value
  305.                             slotValue.Value = nil
  306.                         end
  307.                     end
  308.                 end
  309.                
  310.                 local newItemValue = Instance.new("ObjectValue")
  311.                 newItemValue.Value = toolToInventory
  312.                 newItemValue.Name = toolToInventory.Name
  313.                 newItemValue.Parent = inventory
  314.                
  315.                 break
  316.             end
  317.         end
  318.     end
  319. end)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement