Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local customInv_RS = game.ReplicatedStorage.CustomInventory
- local invSettings = customInv_RS.Settings
- local numHotbarSlots = invSettings.NumHotbarSlots.Value
- local hotbarFolderName = invSettings.HotbarFolderName.Value
- local inventoryFolderName = invSettings.InventoryFolderName.Value
- local dropToolDistance = invSettings.DropToolDistance.Value
- local backpackFolderName = invSettings.BackpackFolderName.Value
- local remoteEvents = customInv_RS.RemoteEvents
- local dropToolRE = remoteEvents.DropTool
- local equipToolRE = remoteEvents.EquipTool
- local swapSlotsRE = remoteEvents.SwapSlots
- local unequipToolRE = remoteEvents.UnequipTool
- local moveToEndOfBackpackRE = remoteEvents.MoveToEndOfBackpack
- -- supprimer les objets jetés
- function getTools(backpack, character)
- local tools = {}
- if backpack then
- tools = backpack:GetChildren()
- else
- warn("[CustomInventory] getTools: backpack argument was nil.")
- end
- if character then
- local characterTool = character:FindFirstChildOfClass("Tool")
- if characterTool then
- if type(tools) ~= "table" then tools = {} end
- table.insert(tools, characterTool)
- end
- end
- return tools
- end
- function findValueFromTool(hotbarFolder, backpackFolder, tool)
- if hotbarFolder then
- for _, value in hotbarFolder:GetChildren() do
- if value.Value == tool then return value end
- end
- end
- if backpackFolder then
- for _, value in backpackFolder:GetChildren() do
- if value.Value == tool then return value end
- end
- end
- end
- function addNewBackpackValue(backpackFolder, tool)
- if not backpackFolder then
- warn("[CustomInventory] addNewBackpackValue: backpackFolder argument was nil.")
- return
- end
- local numBackpackValues = #backpackFolder:GetChildren()
- local newBackpackValue = Instance.new("ObjectValue")
- newBackpackValue.Name = tostring(numBackpackValues + 1)
- newBackpackValue.Value = tool
- newBackpackValue.Parent = backpackFolder
- end
- function findFreeHotbarSlot(hotbarFolder)
- if not hotbarFolder then
- warn("[CustomInventory] findFreeHotbarSlot: hotbarFolder argument was nil.")
- return
- end
- for i=1, numHotbarSlots do
- local slot = hotbarFolder:FindFirstChild(tostring(i))
- if slot and slot:IsA("ObjectValue") and not slot.Value then
- return slot
- end
- end
- end
- function shiftBackpackValuesDown(backpackFolder, startingInventoryValue)
- if not backpackFolder or not startingInventoryValue then
- warn("[CustomInventory] shiftBackpackValuesDown: Invalid arguments.")
- return
- end
- local currentInventoryNumber = tonumber(startingInventoryValue.Name)
- if not currentInventoryNumber then
- warn("[CustomInventory] shiftBackpackValuesDown: startingInventoryValue.Name is not a number: " .. startingInventoryValue.Name)
- return
- end
- currentInventoryNumber = currentInventoryNumber + 1
- local nextSlot = backpackFolder:FindFirstChild(tostring(currentInventoryNumber))
- while nextSlot do
- local prevSlot = backpackFolder:FindFirstChild(tostring(currentInventoryNumber - 1))
- if prevSlot and prevSlot:IsA("ObjectValue") and nextSlot:IsA("ObjectValue") then
- prevSlot.Value = nextSlot.Value
- end
- currentInventoryNumber += 1
- nextSlot = backpackFolder:FindFirstChild(tostring(currentInventoryNumber))
- end
- local lastShiftedSlot = backpackFolder:FindFirstChild(tostring(currentInventoryNumber - 1))
- if lastShiftedSlot then
- lastShiftedSlot:Destroy()
- end
- end
- function updatePlayerTools(player)
- if not player then
- warn("[CustomInventory] updatePlayerTools: déf sur nil.")
- return
- end
- local playerTools = getTools(player:FindFirstChildOfClass("Backpack"), player.Character)
- local inventoryFolder = player:FindFirstChild(inventoryFolderName)
- if not inventoryFolder then return end
- local hotbarFolder = inventoryFolder:FindFirstChild(hotbarFolderName)
- local backpackFolder = inventoryFolder:FindFirstChild(backpackFolderName)
- if hotbarFolder then
- for _, value in hotbarFolder:GetChildren() do
- if value:IsA("ObjectValue") then
- local tool = value.Value
- if tool and not table.find(playerTools, tool) then
- value.Value = nil
- end
- end
- end
- end
- if backpackFolder then
- local itemsToShift = {}
- for _, value in backpackFolder:GetChildren() do
- if value:IsA("ObjectValue") then
- local tool = value.Value
- if tool and not table.find(playerTools, tool) then
- table.insert(itemsToShift, value)
- end
- end
- end
- table.sort(itemsToShift, function(a,b) return tonumber(a.Name) > tonumber(b.Name) end)
- for _, valueToRemove in itemsToShift do
- shiftBackpackValuesDown(backpackFolder, valueToRemove)
- end
- end
- for _, tool in playerTools do
- if not findValueFromTool(hotbarFolder, backpackFolder, tool) then
- local freeHotbarSlot = findFreeHotbarSlot(hotbarFolder)
- if freeHotbarSlot then
- freeHotbarSlot.Value = tool
- else
- addNewBackpackValue(backpackFolder, tool)
- end
- end
- end
- end
- function clearValues(player)
- if not player then
- warn("[CustomInventory] clearValues: player argument was nil.")
- return
- end
- local invFolder = player:FindFirstChild(inventoryFolderName)
- if not invFolder then return end
- local hotbarFolder = invFolder:FindFirstChild(hotbarFolderName)
- if hotbarFolder then
- for _, value in hotbarFolder:GetChildren() do
- if value:IsA("ObjectValue") then
- value.Value = nil
- end
- end
- end
- local backpackFolder = invFolder:FindFirstChild(backpackFolderName)
- if backpackFolder then
- for _, value in backpackFolder:GetChildren() do
- value:Destroy()
- end
- end
- end
- function trackPlayerTools(player)
- local function updateThisPlayerTools()
- updatePlayerTools(player)
- end
- task.wait()
- updateThisPlayerTools()
- player.CharacterAdded:Connect(function(characterInstance)
- task.wait()
- clearValues(player)
- updateThisPlayerTools()
- local backpack = player:FindFirstChildOfClass("Backpack")
- if backpack then
- backpack.ChildAdded:Connect(updateThisPlayerTools)
- backpack.ChildRemoved:Connect(updateThisPlayerTools)
- end
- characterInstance.ChildAdded:Connect(updateThisPlayerTools)
- characterInstance.ChildRemoved:Connect(updateThisPlayerTools)
- end)
- end
- -- créer l'inventaire du joueur
- function createInvFolder(player)
- if player:FindFirstChild(inventoryFolderName) then return end
- local invFolder = Instance.new("Folder")
- invFolder.Name = inventoryFolderName
- local hotbarFolderInstance = Instance.new("Folder")
- hotbarFolderInstance.Name = hotbarFolderName
- hotbarFolderInstance.Parent = invFolder
- for i=1, numHotbarSlots do
- local hotBarSlotValue = Instance.new("ObjectValue")
- hotBarSlotValue.Name = tostring(i)
- hotBarSlotValue.Parent = hotbarFolderInstance
- end
- local backpackFolderInstance = Instance.new("Folder")
- backpackFolderInstance.Name = backpackFolderName
- backpackFolderInstance.Parent = invFolder
- invFolder.Parent = player
- end
- function onPlayerAdded(player)
- createInvFolder(player)
- trackPlayerTools(player)
- end
- game.Players.PlayerAdded:Connect(onPlayerAdded)
- for _, player in game.Players:GetPlayers() do
- onPlayerAdded(player)
- end
- function validateToolParameter(player, tool) -- prévention des hacks
- if typeof(tool) ~= "Instance" or not tool:IsA("Tool") then
- return false
- end
- if tool.Parent ~= player.Backpack and tool.Parent ~= player.Character then
- return false
- end
- return true
- end
- function validateSlot(player, slot)
- if typeof(slot) ~= "Instance" or not slot:IsA("ObjectValue") then
- return false
- end
- local inventoryFolder = player[inventoryFolderName]
- local hotbarFolder = inventoryFolder[hotbarFolderName]
- local backpackFolder = inventoryFolder[backpackFolderName]
- return slot.Parent == hotbarFolder or slot.Parent == backpackFolder
- end
- function unequipTool(player)
- local character = player.Character
- if not character then return end
- local tool = character:FindFirstChildOfClass("Tool")
- if not tool then return end
- tool.Parent = player.Backpack
- end
- unequipToolRE.OnServerEvent:Connect(unequipTool)
- function equipTool(player, tool)
- local character = player.Character
- unequipTool(player)
- tool.Parent = character
- end
- equipToolRE.OnServerEvent:Connect(function(player, tool)
- local character = player.Character
- if not character then return end
- if not validateToolParameter(player, tool) then return end
- equipTool(player, tool)
- end)
- function dropTool(player, tool)
- local inventoryFolder = player[inventoryFolderName]
- local hotbarFolder = inventoryFolder[hotbarFolderName]
- local backpackFolder = inventoryFolder[backpackFolderName]
- local slot = findValueFromTool(hotbarFolder, backpackFolder, tool)
- if slot.Parent == backpackFolder then
- shiftBackpackValuesDown(backpackFolder, slot)
- else
- slot.Value = nil
- end
- local character = player.Character
- local hrp = character.HumanoidRootPart
- local hrpCFrame = hrp.CFrame
- local positionOffset = (hrpCFrame.LookVector * Vector3.new(1, 0, 1).Unit) * dropToolDistance
- local toolCFrame = hrpCFrame + positionOffset
- tool.Parent = workspace
- tool.Handle.CFrame = toolCFrame
- end
- dropToolRE.OnServerEvent:Connect(function(player, tool)
- if not validateToolParameter(player, tool) then return end
- dropTool(player, tool)
- end)
- function swapSlots(player, slot1, slot2)
- local slot1old = slot1.Value
- slot1.Value = slot2.Value
- slot2.Value = slot1old
- local inventoryFolder = player[inventoryFolderName]
- local backpackFolder = inventoryFolder[backpackFolderName]
- if slot1.Parent == backpackFolder and not slot1.Value then
- shiftBackpackValuesDown(backpackFolder, slot1)
- elseif slot2.Parent == backpackFolder and not slot2.Value then
- shiftBackpackValuesDown(backpackFolder, slot2)
- end
- end
- swapSlotsRE.OnServerEvent:Connect(function(player, slot1, slot2)
- if slot1 == slot2 then return end
- if not validateSlot(player, slot1) or not validateSlot(player, slot2) then return end
- swapSlots(player, slot1, slot2)
- end)
- function moveToEndOfBackpack(player, slot)
- local inventoryFolder = player[inventoryFolderName]
- local backpackFolder = inventoryFolder[backpackFolderName]
- addNewBackpackValue(backpackFolder, slot.Value)
- slot.Value = nil
- if slot.Parent == backpackFolder then
- shiftBackpackValuesDown(backpackFolder, slot)
- end
- end
- moveToEndOfBackpackRE.OnServerEvent:Connect(function(player, slot)
- if not validateSlot(player, slot) then return end
- if not slot.Value then return end
- moveToEndOfBackpack(player, slot)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement