Limit tool equiped to only one but

Hello all, first sorry if my English is not perfect it is not my native language.
So, I show you directly my script :

local players = game:GetService('Players')
local runService = game:GetService('RunService')

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character) 
		local bp = player:WaitForChild('Backpack')
		bp.ChildAdded:Connect(function(object) 
			if #bp:GetChildren() < 2 then return end
			runService.Heartbeat:Wait()
			object:Remove()
		end)
	end)
end)

This script works and allows me to have only one item equipped (the one I take after is immediately destroyed). But what I would like is that the new item I take can replace the other item instead of being destroyed. If anyone knows if I can make a modification from this script that would allow me to do this? Thanks a lot

2 Likes

Could you show a video of whats happening so we could se whats happening in more detail?

Here is the video

After taking the first item, I would like to take the other item and have it replace the first item. Instead, the new item I click on gets destroyed directly

no one has any idea how I could proceed so that the item I take replaces the item that is already equipped? please

Did you try: if #bp:GetChildren() < 3 … ?

You could check if the player equips already a tool by checking if he has a tool in his character.

And if he equips that tool, destroy it and then give him the second tool.

He already found a way to fix it.

I will post the solution, it is one script

This script was not written by me but by @domboss37 , Thanks to him.
Local Script in StarterCharacterScript :

local char = script.Parent
local player = game.Players.LocalPlayer

local backpack = player:WaitForChild("Backpack")

backpack.ChildAdded:Connect(function(newChild) -- Listen for children Being added to the backpack
	for _, child in pairs(player.Character:GetChildren()) do -- When tools get equipped they go to the character, so check the character for equipped tools
		if child:IsA("Tool") and child ~= newChild  then child:Destroy() end -- Destroy any other tools
	end

	for _, child in pairs(backpack:GetChildren()) do -- Go throught the backpack
		if child == newChild then continue end -- If the current tool is the new tool skip the loop iteration
		child:Destroy() -- Else destroy the tool
	end
end)

-- Do the same thing here but for when a tool gets equipped in the player (or if you pick a tool up and it gets equipped automatically)

char.ChildAdded:Connect(function(newChild)
	for _, child in pairs(char:GetChildren()) do
		if child:IsA("Tool") and child ~= newChild then child:Destroy() end
	end

	for _, child in pairs(backpack:GetChildren()) do
		if child == newChild then continue end
		child:Destroy()
	end
end)
4 Likes