Advertisement
Sehr_gut

InventoryServer script

Jun 3rd, 2025
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.36 KB | None | 0 0
  1. local customInv_RS = game.ReplicatedStorage.CustomInventory
  2.  
  3. local invSettings = customInv_RS.Settings
  4. local numHotbarSlots = invSettings.NumHotbarSlots.Value
  5. local hotbarFolderName = invSettings.HotbarFolderName.Value
  6. local inventoryFolderName = invSettings.InventoryFolderName.Value
  7. local dropToolDistance = invSettings.DropToolDistance.Value
  8. local backpackFolderName = invSettings.BackpackFolderName.Value
  9.  
  10. local remoteEvents = customInv_RS.RemoteEvents
  11. local dropToolRE = remoteEvents.DropTool
  12. local equipToolRE = remoteEvents.EquipTool
  13. local swapSlotsRE = remoteEvents.SwapSlots
  14. local unequipToolRE = remoteEvents.UnequipTool
  15. local moveToEndOfBackpackRE = remoteEvents.MoveToEndOfBackpack
  16.  
  17.  
  18. -- supprimer les objets jetés
  19.  
  20. function getTools(backpack, character)
  21.     local tools = {}
  22.     if backpack then
  23.         tools = backpack:GetChildren()
  24.     else
  25.         warn("[CustomInventory] getTools: backpack argument was nil.")
  26.     end
  27.     if character then
  28.         local characterTool = character:FindFirstChildOfClass("Tool")
  29.         if characterTool then
  30.             if type(tools) ~= "table" then tools = {} end
  31.             table.insert(tools, characterTool)
  32.         end
  33.     end
  34.     return tools
  35. end
  36.  
  37. function findValueFromTool(hotbarFolder, backpackFolder, tool)
  38.     if hotbarFolder then
  39.         for _, value in hotbarFolder:GetChildren() do
  40.             if value.Value == tool then return value end
  41.         end
  42.     end
  43.     if backpackFolder then
  44.         for _, value in backpackFolder:GetChildren() do
  45.             if value.Value == tool then return value end
  46.         end
  47.     end
  48. end
  49.  
  50. function addNewBackpackValue(backpackFolder, tool)
  51.     if not backpackFolder then
  52.         warn("[CustomInventory] addNewBackpackValue: backpackFolder argument was nil.")
  53.         return
  54.     end
  55.     local numBackpackValues = #backpackFolder:GetChildren()
  56.     local newBackpackValue = Instance.new("ObjectValue")
  57.     newBackpackValue.Name = tostring(numBackpackValues + 1)
  58.     newBackpackValue.Value = tool
  59.     newBackpackValue.Parent = backpackFolder
  60. end
  61.  
  62. function findFreeHotbarSlot(hotbarFolder)
  63.     if not hotbarFolder then
  64.         warn("[CustomInventory] findFreeHotbarSlot: hotbarFolder argument was nil.")
  65.         return
  66.     end
  67.     for i=1, numHotbarSlots do
  68.         local slot = hotbarFolder:FindFirstChild(tostring(i))
  69.         if slot and slot:IsA("ObjectValue") and not slot.Value then
  70.             return slot
  71.         end
  72.     end
  73. end
  74.  
  75. function shiftBackpackValuesDown(backpackFolder, startingInventoryValue)
  76.     if not backpackFolder or not startingInventoryValue then
  77.         warn("[CustomInventory] shiftBackpackValuesDown: Invalid arguments.")
  78.         return
  79.     end
  80.     local currentInventoryNumber = tonumber(startingInventoryValue.Name)
  81.     if not currentInventoryNumber then
  82.         warn("[CustomInventory] shiftBackpackValuesDown: startingInventoryValue.Name is not a number: " .. startingInventoryValue.Name)
  83.         return
  84.     end
  85.     currentInventoryNumber = currentInventoryNumber + 1
  86.     local nextSlot = backpackFolder:FindFirstChild(tostring(currentInventoryNumber))
  87.     while nextSlot do
  88.         local prevSlot = backpackFolder:FindFirstChild(tostring(currentInventoryNumber - 1))
  89.         if prevSlot and prevSlot:IsA("ObjectValue") and nextSlot:IsA("ObjectValue") then
  90.             prevSlot.Value = nextSlot.Value
  91.         end
  92.         currentInventoryNumber += 1
  93.         nextSlot = backpackFolder:FindFirstChild(tostring(currentInventoryNumber))
  94.     end
  95.     local lastShiftedSlot = backpackFolder:FindFirstChild(tostring(currentInventoryNumber - 1))
  96.     if lastShiftedSlot then
  97.         lastShiftedSlot:Destroy()
  98.     end
  99. end
  100.  
  101. function updatePlayerTools(player)
  102.     if not player then
  103.         warn("[CustomInventory] updatePlayerTools: déf sur nil.")
  104.         return
  105.     end
  106.     local playerTools = getTools(player:FindFirstChildOfClass("Backpack"), player.Character)
  107.     local inventoryFolder = player:FindFirstChild(inventoryFolderName)
  108.     if not inventoryFolder then return end
  109.     local hotbarFolder = inventoryFolder:FindFirstChild(hotbarFolderName)
  110.     local backpackFolder = inventoryFolder:FindFirstChild(backpackFolderName)
  111.    
  112.     if hotbarFolder then
  113.         for _, value in hotbarFolder:GetChildren() do
  114.             if value:IsA("ObjectValue") then
  115.                 local tool = value.Value
  116.                 if tool and not table.find(playerTools, tool) then
  117.                     value.Value = nil
  118.                 end
  119.             end
  120.         end
  121.     end
  122.     if backpackFolder then
  123.         local itemsToShift = {}
  124.         for _, value in backpackFolder:GetChildren() do
  125.             if value:IsA("ObjectValue") then
  126.                 local tool = value.Value
  127.                 if tool and not table.find(playerTools, tool) then
  128.                     table.insert(itemsToShift, value)
  129.                 end
  130.             end
  131.         end
  132.         table.sort(itemsToShift, function(a,b) return tonumber(a.Name) > tonumber(b.Name) end)
  133.         for _, valueToRemove in itemsToShift do
  134.             shiftBackpackValuesDown(backpackFolder, valueToRemove)
  135.         end
  136.     end
  137.     for _, tool in playerTools do
  138.         if not findValueFromTool(hotbarFolder, backpackFolder, tool) then
  139.             local freeHotbarSlot = findFreeHotbarSlot(hotbarFolder)
  140.             if freeHotbarSlot then
  141.                 freeHotbarSlot.Value = tool
  142.             else
  143.                 addNewBackpackValue(backpackFolder, tool)
  144.             end
  145.         end
  146.     end
  147. end
  148.  
  149. function clearValues(player)
  150.     if not player then
  151.         warn("[CustomInventory] clearValues: player argument was nil.")
  152.         return
  153.     end
  154.     local invFolder = player:FindFirstChild(inventoryFolderName)
  155.     if not invFolder then return end
  156.     local hotbarFolder = invFolder:FindFirstChild(hotbarFolderName)
  157.     if hotbarFolder then
  158.         for _, value in hotbarFolder:GetChildren() do
  159.             if value:IsA("ObjectValue") then
  160.                 value.Value = nil
  161.             end
  162.         end
  163.     end
  164.     local backpackFolder = invFolder:FindFirstChild(backpackFolderName)
  165.     if backpackFolder then
  166.         for _, value in backpackFolder:GetChildren() do
  167.             value:Destroy()
  168.         end
  169.     end
  170. end
  171.  
  172. function trackPlayerTools(player)
  173.     local function updateThisPlayerTools()
  174.         updatePlayerTools(player)
  175.     end
  176.     task.wait()
  177.     updateThisPlayerTools()
  178.     player.CharacterAdded:Connect(function(characterInstance)
  179.         task.wait()
  180.         clearValues(player)
  181.         updateThisPlayerTools()
  182.         local backpack = player:FindFirstChildOfClass("Backpack")
  183.         if backpack then
  184.             backpack.ChildAdded:Connect(updateThisPlayerTools)
  185.             backpack.ChildRemoved:Connect(updateThisPlayerTools)
  186.         end
  187.         characterInstance.ChildAdded:Connect(updateThisPlayerTools)
  188.         characterInstance.ChildRemoved:Connect(updateThisPlayerTools)
  189.     end)
  190. end
  191.  
  192. -- créer l'inventaire du joueur
  193.  
  194. function createInvFolder(player)
  195.     if player:FindFirstChild(inventoryFolderName) then return end
  196.     local invFolder = Instance.new("Folder")
  197.     invFolder.Name = inventoryFolderName
  198.     local hotbarFolderInstance = Instance.new("Folder")
  199.     hotbarFolderInstance.Name = hotbarFolderName
  200.     hotbarFolderInstance.Parent = invFolder
  201.     for i=1, numHotbarSlots do
  202.         local hotBarSlotValue = Instance.new("ObjectValue")
  203.         hotBarSlotValue.Name = tostring(i)
  204.         hotBarSlotValue.Parent = hotbarFolderInstance
  205.     end
  206.     local backpackFolderInstance = Instance.new("Folder")
  207.     backpackFolderInstance.Name = backpackFolderName
  208.     backpackFolderInstance.Parent = invFolder
  209.     invFolder.Parent = player
  210. end
  211.  
  212. function onPlayerAdded(player)
  213.     createInvFolder(player)
  214.     trackPlayerTools(player)
  215. end
  216.  
  217. game.Players.PlayerAdded:Connect(onPlayerAdded)
  218.  
  219. for _, player in game.Players:GetPlayers() do
  220.     onPlayerAdded(player)
  221. end
  222.  
  223. function validateToolParameter(player, tool) -- prévention des hacks
  224.     if typeof(tool) ~= "Instance" or not tool:IsA("Tool") then
  225.         return false
  226.     end
  227.  
  228.     if tool.Parent ~= player.Backpack and tool.Parent ~= player.Character then
  229.         return false
  230.     end
  231.  
  232.     return true
  233. end
  234.  
  235. function validateSlot(player, slot)
  236.     if typeof(slot) ~= "Instance" or not slot:IsA("ObjectValue") then
  237.         return false
  238.     end
  239.    
  240.     local inventoryFolder = player[inventoryFolderName]
  241.     local hotbarFolder = inventoryFolder[hotbarFolderName]
  242.     local backpackFolder = inventoryFolder[backpackFolderName]
  243.    
  244.     return slot.Parent == hotbarFolder or slot.Parent == backpackFolder
  245. end
  246.  
  247. function unequipTool(player)
  248.     local character = player.Character
  249.     if not character then return end
  250.    
  251.     local tool = character:FindFirstChildOfClass("Tool")
  252.     if not tool then return end
  253.    
  254.     tool.Parent = player.Backpack
  255. end
  256.  
  257. unequipToolRE.OnServerEvent:Connect(unequipTool)
  258.  
  259. function equipTool(player, tool)
  260.     local character = player.Character
  261.     unequipTool(player)
  262.    
  263.     tool.Parent = character
  264. end
  265.  
  266. equipToolRE.OnServerEvent:Connect(function(player, tool)
  267.     local character = player.Character
  268.     if not character then return end
  269.    
  270.     if not validateToolParameter(player, tool) then return end
  271.    
  272.     equipTool(player, tool)
  273. end)
  274.  
  275. function dropTool(player, tool)
  276.     local inventoryFolder = player[inventoryFolderName]
  277.     local hotbarFolder = inventoryFolder[hotbarFolderName]
  278.     local backpackFolder = inventoryFolder[backpackFolderName]
  279.    
  280.     local slot = findValueFromTool(hotbarFolder, backpackFolder, tool)
  281.    
  282.     if slot.Parent == backpackFolder then
  283.         shiftBackpackValuesDown(backpackFolder, slot)
  284.     else
  285.         slot.Value = nil
  286.     end
  287.    
  288.     local character = player.Character
  289.     local hrp = character.HumanoidRootPart
  290.    
  291.     local hrpCFrame = hrp.CFrame
  292.     local positionOffset = (hrpCFrame.LookVector * Vector3.new(1, 0, 1).Unit) * dropToolDistance
  293.    
  294.     local toolCFrame = hrpCFrame + positionOffset
  295.     tool.Parent = workspace
  296.     tool.Handle.CFrame = toolCFrame
  297. end
  298.  
  299. dropToolRE.OnServerEvent:Connect(function(player, tool)
  300.     if not validateToolParameter(player, tool) then return end
  301.    
  302.     dropTool(player, tool)
  303. end)
  304.  
  305. function swapSlots(player, slot1, slot2)
  306.     local slot1old = slot1.Value
  307.     slot1.Value = slot2.Value
  308.     slot2.Value = slot1old
  309.    
  310.     local inventoryFolder = player[inventoryFolderName]
  311.     local backpackFolder = inventoryFolder[backpackFolderName]
  312.    
  313.     if slot1.Parent == backpackFolder and not slot1.Value then
  314.         shiftBackpackValuesDown(backpackFolder, slot1)
  315.     elseif slot2.Parent == backpackFolder and not slot2.Value then
  316.         shiftBackpackValuesDown(backpackFolder, slot2)
  317.     end
  318. end
  319.  
  320. swapSlotsRE.OnServerEvent:Connect(function(player, slot1, slot2)
  321.     if slot1 == slot2 then return end
  322.    
  323.     if not validateSlot(player, slot1) or not validateSlot(player, slot2) then return end
  324.      
  325.     swapSlots(player, slot1, slot2)
  326. end)
  327.  
  328. function moveToEndOfBackpack(player, slot)
  329.     local inventoryFolder = player[inventoryFolderName]
  330.     local backpackFolder = inventoryFolder[backpackFolderName]
  331.    
  332.     addNewBackpackValue(backpackFolder, slot.Value)
  333.     slot.Value = nil
  334.    
  335.     if slot.Parent == backpackFolder then
  336.         shiftBackpackValuesDown(backpackFolder, slot)
  337.     end
  338. end
  339.  
  340. moveToEndOfBackpackRE.OnServerEvent:Connect(function(player, slot)
  341.     if not validateSlot(player, slot) then return end
  342.     if not slot.Value then return end
  343.    
  344.     moveToEndOfBackpack(player, slot)
  345. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement