I’m trying to make an inventory system where if the players has more than 6 tools, it prevents the player from getting more tools. However when a player equips and then un-equips a tool, it increases the value. It needs to make sure the previous parent wasn’t the character so it can increase the value the desired way.
player.Backpack.ChildAdded:Connect(function(tool)
tool.AncestryChanged:Connect(function(child, parent)
if child.Name ~= "Tablet" then
player.ToolAmount.Value += 1
end
end)
end)
So what you can do is that you can first check if the child that being added to you backpack is a tool, then using for loop to loop through the Backpack folder to see how many tools you currently have instead of manually counting it yourself. Here is the code that you can start with.
player.Backpack.ChildAdded:Connect(function(child)
if child:IsA(“Tool”) then
— loop through the folder.
end
end)
player.Backpack.ChildRemoved:Connect(function(tool)
tool.AncestryChanged:Connect(function(child, parent)
if child.Parent.Name == "Workspace" and child.Name ~= "Tablet" then
player.ToolAmount.Value -= 1
end
end)
end)
What if you put an Int NumberValue in the backpack as well and use it to set the number of the items in the backpack.
When you equip or unequip a tool don’t +1 or -1 it from that value, only -1 when an item is dropped or + 1 when an object is picked up. If the number is ‘full’ then don’t allow the player to pick up another item.