Backpack Tool Limit

Super quick and easy question: Can you make it so when a player gets a new tool in their inventory it deletes the old tool? (So there is a one tool limit in inventory)

Tried searching other posts they were slightly different from what I want.

Pretty new to scripting so this helps a lot.

5 Likes

You can get the number of tools in the backpack with

local tools = player.Backpack:GetChildren()

You can get if the player has a tool equipped with

local hasToolEquipped
do
    local character = player.Character
    if character and character:FindFirstChildOfClass("Tool") then
        hasToolEquipped= true
    end
end

You can then get the tool count like:

local toolCount
do
    toolCount += #tools
    if hasToolEquipped then
        toolCount += 1
    end
end

Then all you need to do is connect to Player.Backpack.ChildAdded and Player.Character.ChildAdded and check the tool count. If there are too many, you Destroy the old one:

local MAX_TOOLS = 3

local function checkTools(tool)
    -- get tool count here
    if toolCount > MAX_TOOLS then
        tool:Destroy()
    end
end

player.Backpack.ChildAdded:Connect(checkTools)

player.CharacterAdded:Connect(function(character)
    character.ChildAdded:Connect(function(child))
        if child:IsA("Tool") then
            checkTools(child)
        end
    end
end)

Then just run that code every time a player is added a connection to game:GetService("Players").PlayerAdded!

Let me know if you’ve got any questions :happy2:

6 Likes

Recent post about this topic.

1 Like

You can make a function that checks the player backpack and the character

Use loop to check the character has a tool and destroy it (checking if the player equipped a tool)

Use a loop to destroy the player backpack children

Then clone your new tool to the player backpack

Sorry if i didnt provide the code, im outside of my house and use a phone

I’m not sure what you mean. The stuff I outlined above would work for a single player. To do that for every player, you’d probably want to run it on every player who joins the game with Player.PlayerAdded.

1 Like
--Script or LocalScript inside StarterCharacterScripts
--depending on if you want it to replicate or no
local Players = game:GetService("Players")

local limit = 2 --max tools

local Character = script.Parent
local Player = Players:GetPlayerFromCharacter(Character)
local Backpack = Player:WaitForChild("Backpack", 5)
if not Backpack then warn("Backpack wasn't found") return end 

function GetTools()
	local tools = {}
	--search through tools in backpack
	for _, tool in ipairs(Player.Backpack:GetChildren()) do 
		table.insert(tools, tool)
	end
	--get the equipped tool(if there is one)
	local equipped = Character:FindFirstChildWhichIsA("Tool")
	table.insert(tools, equipped) 
	return tools 
end

--This function fights the "Something unexpectedly tried to set the parent of Tool to NULL" warning. It shows on the console but has no impact
function ForceDestroy(tool)
	repeat 
		tool:Destroy() 
		task.wait()
	until tool.Parent == nil 
end

function Update(new)
	local tools = GetTools() 
	--in case their StarterPack has more than max tools, remove the last from their inventory.
	if not new and #tools > limit then 
		for i = limit+1, #tools do 
			ForceDestroy(tools[i])
		end
		return 
	end
	--if a new tool is added and the limit is passed, destroy it
	if #tools > limit then 
		ForceDestroy(new)
	end
end

Update()
Backpack.ChildAdded:Connect(Update)
Character.ChildAdded:Connect(function(child)
	if child:IsA("Tool") then 
		Update(child)
	end
end)
2 Likes