Max tools inside Backpack

Basicly I want to make one limit for the backpack if the player had 3 tools inside the backpack the GUI will say “inventory full” and the tool will not clone.

I tried a lot of things but doesnt work.

----Original Script---
equip.MouseButton1Click:Connect(function()
	if selected == "classic" then
		if tool1Equipped == false then
			tool1Equipped = true
			tool1:Clone().Parent = player.Backpack
			RS.ToolsCloneRemoteEvents.ClassicSwordEvent:FireServer("ClassicSword")
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool1Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("ClassicSword"):Destroy()
			player.StarterGear:FindFirstChild("ClassicSword"):Destroy()
		end
	end
end)

-----What I tried to make------

local player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local toolsRS = RS.Tools
local scrollFrameMelee = script.Parent.Parent.Meeles


local equip = script.Parent.ToolButton
local ToolNameText = script.Parent.ToolName
local ToolDescp = script.Parent.ToolDescription
local ToolImage = script.Parent.ToolImage
local limit = 0
local selected = nil

local tool1Equipped = false

equip.MouseButton1Click:Connect(function()
	if selected == "classic" then
		if tool1Equipped == false then
			tool1Equipped = true
			tool1:Clone().Parent = player.Backpack
			RS.ToolsCloneRemoteEvents.ClassicSwordEvent:FireServer("ClassicSword")
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
elseif limit >= 3 then
			equip.Text = "BACKPACK FULL!" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool1Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("ClassicSword"):Destroy()
			player.StarterGear:FindFirstChild("ClassicSword"):Destroy()
		end
	end
end)

showcase:

you can see on the gif I changed the limit to 4 and nothing happen.

4 Likes
local player = -- wherever the player comes from
local character = player.Character
local toolsinBackpack = 0

-- check if the player has a tool in their backpack or character
for _, Tool in pairs(character:GetChildren() do 
         if Tool:IsA("Tool") then
               toolsinBackpack = toolsinBackpack + 1
        end
end
for _, Tool in pairs(player.Backpack:GetChildren() do
         if Tool:IsA("Tool") then
               toolsinBackpack = toolsinBackpack + 1
        end
end

if toolsinBackpack <= 2 then
-- player does not have 3 or more tools
game.ReplicatedStorage.Tool:Clone().Parent = player.Backpack
else
-- player has 3 or more tools
end

this should work i think

I mean I want to do that inside the function. And I was trying to do something difrent.

The script I am trying to do now.

local player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local leaderstats = player:WaitForChild("toolLeaderstats")
local toolsRS = RS.Tools
local scrollFrameMelee = script.Parent.Parent.Meeles
local scrollFrameGuns = script.Parent.Parent.Guns

--------Tool Buttons ------
local classic = scrollFrameMelee.ClassicSwordButton
local classicBomb = scrollFrameMelee.ClassicBombButton
local Bagguete = scrollFrameMelee.BaggueteSwordButton
local BanH = scrollFrameMelee.BanHammerButton
local StopSign = scrollFrameMelee.StopSignButton
local Bat = scrollFrameMelee.BatButton
local LondonKnife = scrollFrameMelee.LondonKnifeButton
local FryingPan = scrollFrameMelee.FryingPanButton

local equip = script.Parent.ToolButton
local ToolNameText = script.Parent.ToolName
local ToolDescp = script.Parent.ToolDescription
local ToolImage = script.Parent.ToolImage
local selected = nil
-------- var for equip-----
local tool1Equipped = false
local tool2Equipped = false
local tool3Equipped = false
local tool4Equipped = false
local tool5Equipped = false
local tool6Equipped = false
local tool7Equipped = false
local tool8Equipped = false
---------RS tools-----------
local tool1 = toolsRS.ClassicSword
local tool2 = toolsRS.ClassicBomb
local tool3 = toolsRS.BaggueteSword
local tool4 = toolsRS.BanHammer
local tool5 = toolsRS.StopSign
local tool6 = toolsRS.Bat
local tool7 = toolsRS.LondonKnife
local tool8 = toolsRS.FryingPan
---------------------------
-- Equip button---
limit = 1

local test = false
equip.MouseButton1Click:Connect(function()
	if selected == "classic" then
		if tool1Equipped == false then
			tool1Equipped = true
			tool1:Clone().Parent = player.Backpack
			RS.ToolsCloneRemoteEvents.ClassicSwordEvent:FireServer("ClassicSword")
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool1Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("ClassicSword"):Destroy()
			player.StarterGear:FindFirstChild("ClassicSword"):Destroy()
		end
	end
end)


equip.MouseButton1Click:Connect(function()
	if selected == "classicBomb" then
		if tool2Equipped == false then
			tool2Equipped = true
			limit = limit - 1
			print(limit)
			tool2:Clone().Parent = player.Backpack
			RS.ToolsCloneRemoteEvents.ClassicBombEvent:FireServer("ClassicBomb")
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool2Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("ClassicBomb"):Destroy()
			player.StarterGear:FindFirstChild("ClassicBomb"):Destroy()
		end
	end
end)

equip.MouseButton1Click:Connect(function()
	if selected == "Bagguete" then
		if tool3Equipped == false then
			tool3Equipped = true
			limit = limit + 1
			print(limit)
			tool3:Clone().Parent = player.Backpack
			RS.ToolsCloneRemoteEvents.BaggueteSwordEvent:FireServer("BaggueteSword")
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool3Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("BaggueteSword"):Destroy()
			player.StarterGear:FindFirstChild("BaggueteSword"):Destroy()
		end
	end
end)

equip.MouseButton1Click:Connect(function()
	if selected == "BanH" then
		if tool4Equipped == false then
			tool4Equipped = true
			limit = limit + 1
			print(limit)
			tool4:Clone().Parent = player.Backpack
			RS.ToolsCloneRemoteEvents.BanHammerEvent:FireServer("BanHammer")
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool4Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("BanHammer"):Destroy()
			player.StarterGear:FindFirstChild("BanHammer"):Destroy()
		end
	end
end)

equip.MouseButton1Click:Connect(function()
	if selected == "StopSign" then
		if tool5Equipped == false then
			tool5Equipped = true
			tool5:Clone().Parent = player.Backpack
			RS.ToolsCloneRemoteEvents.StopSignEvent:FireServer("StopSign")
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool5Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("StopSign"):Destroy()
			player.StarterGear:FindFirstChild("StopSign"):Destroy()
		end
	end
end)

equip.MouseButton1Click:Connect(function()
	if selected == "Bat" then
		if tool6Equipped == false then
			tool6Equipped = true
			tool6:Clone().Parent = player.Backpack
			RS.ToolsCloneRemoteEvents.BatEvent:FireServer("Bat")
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool6Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("Bat"):Destroy()
			player.StarterGear:FindFirstChild("Bat"):Destroy()
		end
	end
end)

equip.MouseButton1Click:Connect(function()
	if selected == "LondonKnife" then
		if tool7Equipped == false then
			tool7Equipped = true
			tool7:Clone().Parent = player.Backpack
			RS.ToolsCloneRemoteEvents.LondonKnifeEvent:FireServer("LondonKnife")
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool7Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("LondonKnife"):Destroy()
			player.StarterGear:FindFirstChild("LondonKnife"):Destroy()
		end
	end
end)

equip.MouseButton1Click:Connect(function()
	if selected == "FryingPan" then
		if tool8Equipped == false then
			tool8Equipped = true
			tool8:Clone().Parent = player.Backpack
			RS.ToolsCloneRemoteEvents.FryingPanEvent:FireServer("FryingPan")
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		else
			tool8Equipped = false
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			player.Backpack:FindFirstChild("FryingPan"):Destroy()
			player.StarterGear:FindFirstChild("FryingPan"):Destroy()
		end
	end
end)
-- Change Buttons --
print(limit)
classic.MouseButton1Click:Connect(function()
	selected = "classic"
	if limit == 0 then
		if tool1Equipped == false then
			equip.Text = "Equip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			ToolNameText.Text = "Tool: "..tool1.Name
			ToolDescp.Text = "Description: Classic Roblox sword"
		else 
			equip.Text = "Unequip weapon" 
			equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
			ToolNameText.Text = "Tool: "..tool1.Name
			ToolDescp.Text = "Description: Classic Roblox sword"
		end
	else
		equip.Text = "backpack full!"
		tool1Equipped = false
	end
end)

classicBomb.MouseButton1Click:Connect(function()
	selected = "classicBomb"
	if tool2Equipped == false then
		equip.Text = "Equip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
		ToolNameText.Text = "Tool: "..tool2.Name
		ToolDescp.Text = "Description: Roblox ClassicBomb"
	else
		equip.Text = "Unequip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		ToolNameText.Text = "Tool: "..tool2.Name
		ToolDescp.Text = "Description: Roblox ClassicBomb"
	end
end)

Bagguete.MouseButton1Click:Connect(function()
	selected = "Bagguete"
	if tool3Equipped == false then
		equip.Text = "Equip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
		ToolNameText.Text = "Tool: "..tool3.Name
		ToolDescp.Text = "Description: OUI OUI BAGGUETE"
	else
		equip.Text = "Unequip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		ToolNameText.Text = "Tool: "..tool3.Name
		ToolDescp.Text = "Description: OUI OUI BAGGUETE"
	end
end)

BanH.MouseButton1Click:Connect(function()
	selected = "BanH"
	if tool4Equipped == false then
		equip.Text = "Equip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
		ToolNameText.Text = "Tool: "..tool4.Name
		ToolDescp.Text = "Description: Get banned xd"
	else
		equip.Text = "Unequip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		ToolNameText.Text = "Tool: "..tool4.Name
		ToolDescp.Text = "Description: Get banned xd"
	end
end)

StopSign.MouseButton1Click:Connect(function()
	selected = "StopSign"
	if tool5Equipped == false then
		equip.Text = "Equip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
		ToolNameText.Text = "Tool: "..tool5.Name
		ToolDescp.Text = "Description: S T O P"
	else
		equip.Text = "Unequip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		ToolNameText.Text = "Tool: "..tool5.Name
		ToolDescp.Text = "Description: S T O P"
	end
end)

Bat.MouseButton1Click:Connect(function()
	selected = "Bat"
	if tool6Equipped == false then
		equip.Text = "Equip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
		ToolNameText.Text = "Tool: "..tool6.Name
		ToolDescp.Text = "Description: BONK!"
	else
		equip.Text = "Unequip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		ToolNameText.Text = "Tool: "..tool6.Name
		ToolDescp.Text = "Description: BONK!"
	end
end)

LondonKnife.MouseButton1Click:Connect(function()
	selected = "LondonKnife"
	if tool7Equipped == false then
		equip.Text = "Equip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
		ToolNameText.Text = "Tool: "..tool7.Name
		ToolDescp.Text = "Description: London."
	else
		equip.Text = "Unequip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		ToolNameText.Text = "Tool: "..tool7.Name
		ToolDescp.Text = "Description: London."
	end
end)

FryingPan.MouseButton1Click:Connect(function()
	selected = "FryingPan"
	if tool8Equipped == false then
		equip.Text = "Equip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
		ToolNameText.Text = "Tool: "..tool8.Name
		ToolDescp.Text = "Description: Cool for make eggs and bacon."
	else
		equip.Text = "Unequip weapon" 
		equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		ToolNameText.Text = "Tool: "..tool8.Name
		ToolDescp.Text = "Description: Cool for make eggs and bacon."
	end
end)

I want to change when the player clicks in the button (before clicking in the equip button) but the problem is I tried to change for false for not clone and change again but doesnt work.

I mean

if tool1Equipped == false then
			tool1Equipped = true

can be the issue I dont know.

if you are going to make a equip system, you just add a check when the player is going to equip the tool to see if there is the enough space

for instance

edit: I have this on my equip system and works, should work for you too

local maxnum = 3 --max number of items in backpack

script.Parent.MouseButton1Click:Connect(function()
     if #player.Backpack:GetChildren() < maxnum then
          tool:Clone().Parent = player.Backpack
      end
end)

3 Likes

Ok it works but I have only 1 problem.

you can see on the gif when you hit the limit of tools in the inventory you cant equip yes, but when you unequip and you want to equip other tool doesnt work.

hold up I fixed but I cant equip the classic sword when I got the backpack with 3 tools.


Basicly when the backpack got 3 tools I cant unequip any of this tools.

equip.MouseButton1Click:Connect(function()
	if selected == "classic" then
		if #player.Backpack:GetChildren() < limit.Value then
			if tool1Equipped == false then
				tool1Equipped = true
				tool1:Clone().Parent = player.Backpack
				RS.ToolsCloneRemoteEvents.ClassicSwordEvent:FireServer("ClassicSword")
				equip.Text = "Unequip weapon"
				equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)	
			else
				tool1Equipped = false
				equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
				equip.Text = "Equip weapon" 
				player.Backpack:FindFirstChild("ClassicSword"):Destroy()
				player.StarterGear:FindFirstChild("ClassicSword"):Destroy()
			end
		end
	end
end)

I can make one elseif? or something for it?

Thats because you misplaced the if statements. It should be like this:

equip.MouseButton1Click:Connect(function()
	if selected == "classic" then
		if tool1Equipped == false then
			if #player.Backpack:GetChildren() < limit.Value then
				tool1Equipped = true
				tool1:Clone().Parent = player.Backpack
				RS.ToolsCloneRemoteEvents.ClassicSwordEvent:FireServer("ClassicSword")
				equip.Text = "Unequip weapon"
				equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)	
			end
		else
			tool1Equipped = false
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			equip.Text = "Equip weapon" 
			player.Backpack:FindFirstChild("ClassicSword"):Destroy()
			player.StarterGear:FindFirstChild("ClassicSword"):Destroy()
		end
	end
end)
1 Like

yes and how I can make the text change saying Inventory full without elseif?

1 Like

Ah, if you would like the text to change to “Inventory Full” when clicked then:

equip.MouseButton1Click:Connect(function()
	if selected == "classic" then
		if tool1Equipped == false then
			if #player.Backpack:GetChildren() < limit.Value then
				tool1Equipped = true
				tool1:Clone().Parent = player.Backpack
				RS.ToolsCloneRemoteEvents.ClassicSwordEvent:FireServer("ClassicSword")
				equip.Text = "Unequip weapon"
				equip.BackgroundColor3 = Color3.fromRGB(170, 0, 0)	
			else
				equip.Text = "Inventory Full!" -- this is if you want equip button text to change, modify this line if you want a different item to change its text
				-- Add your code here on what else you want to do when inventory is full
				-- example: changing the color of equip button
			end
		else
			tool1Equipped = false
			equip.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			equip.Text = "Equip weapon" 
			player.Backpack:FindFirstChild("ClassicSword"):Destroy()
			player.StarterGear:FindFirstChild("ClassicSword"):Destroy()
		end
	end
end)
3 Likes

ok one question I should use ServerStorage? Because exploiters can use tools from ReplicatedStorage.

I’m not experienced with preventing exploits yet so I’m not too sure xD. Also please mark the post as Solution if your problem has been solved. Thanks!

1 Like