Help to create inventory limit

I tried to do but not working, soo
image
image

I wanted that when the player has a full inventory, the proximity items that are in the tools do not listen to the player and do not add, and when the player uses a backpack, +3 slots are added

It isn’t very clear how your inventory system works, so I’ll be answering under the assumption that you’re using the backpack (i.e. container object parented to all player objects).

There aren’t any native methods to limiting the capacity of backpacks, so you’ll have to program it in yourself. I would implement it as checking how many items are in a player’s backpack every time they want to add something in, then prevent them from adding anything in if that number is at the capacity. You can get when players are trying to add something in using the backpack’s ChildAdded event. You can get the number of items in a player’s backpack by using the length operator on the returned array of a backpack’s GetChildren method.

I didn’t understand anything, but it’s interesting.

I’ll try to break down what he said.

(Sorry if I did get something wrong)

native methods

(I’m not 100 percent on this so just take this with a grain of salt.)
These are just prebuilt functions like game.Players:GetPlayers()

Child added Function

The name is pretty self-explanatory whenever a child or an object is parent to a object the function fires every single instance has this function. E.g.

local Part = workspace.Part

Part.ChildAdded:Connect(function(addedchild)
    print(addedchild)
end)

length operator

Very useful trick basically any table that you want to have the number of index’s in it you just have # before it.

Code example

local coolTab = {"a", "k"}
print(#coolTab) -- prints 2

An example for you that may be useful for your understanding (assuming you are using the default Roblox backpack

local Players = game: GetService("Players")

local Player = Players.LocalPlayer
local char = Player.Character
local backpack = char.Backpack

local MaxItems = 20

backpack.ChildAdded:Connect(function(addedchild)
  local invsize = #backpack:Getchildren() 
  -- the get children returns a table of all of the instances inside of the object that you can loop though,
  -- but for our case we want the amount items
  print(invsize) -- if their are a sword and gun in there it would print 2
  -- we now see if it exceeds the max amount of items
  if invsize > MaxItems then
  print("too many items removing", addedchild)
   -- remove the object what will be completed on the server
    return
  end
  -- if all of that passes then we set the text to the correct amount of space
end)

If there is anything else you would like help with just ask me.
Thank you for reading i hope this gave you a basic understanding on this.

You explained a complex example to me, but I only understood

local Players = game: GetService("Players")

local Player = Players.LocalPlayer
local char = Player.Character
local backpack = char.Backpack

local MaxItems = 20

backpack.ChildAdded:Connect(function(addedchild)
  local invsize = #backpack:Getchildren() 
  -- the get children returns a table of all of the instances inside of the object that you can loop though,
  -- but for our case we want the amount items
  print(invsize) -- if their are a sword and gun in there it would print 2
  -- we now see if it exceeds the max amount of items
  if invsize > MaxItems then
  print("too many items removing", addedchild)
   -- remove the object what will be completed on the server
    return
  end
  -- if all of that passes then we set the text to the correct amount of space
end)
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
local gui = player:WaitForChild("PlayerGui"):WaitForChild("CombatGUI")
local InvText = gui:WaitForChild("InventoryFrame"):WaitForChild("InvText")

local BASE_LIMIT = 2
local EXTRA_SLOT_TOOL_NAME = "Backpack"
local EXTRA_SLOTS = 3

local function getAllHeldTools()
	local tools = {}

	for _, tool in ipairs(player.Backpack:GetChildren()) do
		if tool:IsA("Tool") and tool.Name ~= EXTRA_SLOT_TOOL_NAME then
			table.insert(tools, tool)
		end
	end

	local character = player.Character
	if character then
		for _, tool in ipairs(character:GetChildren()) do
			if tool:IsA("Tool") and tool.Name ~= EXTRA_SLOT_TOOL_NAME then
				table.insert(tools, tool)
			end
		end
	end

	return tools
end

local function getExtraSlots()
	local count = 0

	for _, tool in ipairs(player.Backpack:GetChildren()) do
		if tool:IsA("Tool") and tool.Name == EXTRA_SLOT_TOOL_NAME then
			count += 1
		end
	end

	if player.Character then
		for _, tool in ipairs(player.Character:GetChildren()) do
			if tool:IsA("Tool") and tool.Name == EXTRA_SLOT_TOOL_NAME then
				count += 1
			end
		end
	end

	return count * EXTRA_SLOTS
end

local function updateInvText()
	local current = #getAllHeldTools()
	local max = BASE_LIMIT + getExtraSlots()
	InvText.Text = string.format("Backpack [%d/%d]", current, max)
end


local promptConnections = {}

local function disconnectAllPromptConnections()
	for prompt, connection in pairs(promptConnections) do
		connection:Disconnect()
	end
	promptConnections = {}
end

local function setupPromptInterception()
	disconnectAllPromptConnections()

	local current = #getAllHeldTools()
	local max = BASE_LIMIT + getExtraSlots()
	local full = current >= max

	for _, tool in ipairs(workspace:GetDescendants()) do
		if tool:IsA("Tool") and not tool:IsDescendantOf(player.Character) and not tool:IsDescendantOf(player.Backpack) then
			for _, descendant in ipairs(tool:GetDescendants()) do
				if descendant:IsA("ProximityPrompt") and descendant.Name == "PickUp" then
					local conn = descendant.Triggered:Connect(function(plr)
						if plr == player then
							if full then
								print("Инвентарь полный — не можем взять предмет")
								return
							else
							end
						end
					end)
					promptConnections[descendant] = conn
				end
			end
		end
	end
end

local function enforceLimit()
	local tools = getAllHeldTools()
	local max = BASE_LIMIT + getExtraSlots()

	for i = max + 1, #tools do
		tools[i]:Destroy()
	end

	updateInvText()
	setupPromptInterception()
end

local function connectEvents()
	local function onChar(char)
		char.ChildAdded:Connect(enforceLimit)
		char.ChildRemoved:Connect(enforceLimit)
	end

	if player.Character then
		onChar(player.Character)
	end

	player.CharacterAdded:Connect(onChar)

	backpack.ChildAdded:Connect(enforceLimit)
	backpack.ChildRemoved:Connect(enforceLimit)
end

task.spawn(function()
	while true do
		enforceLimit()
		task.wait(0.5)
	end
end)

connectEvents()
enforceLimit()

I have this script but - in that when the inventory is full tools are taken but already disappear, and I’m thinking how to make it so that it doesn’t disappear and generally the event fails if the inventory is full