Help with detecting backpack

I need help figuring out why it adds the tool if you equip or unequip the tool. I only want to add the tool to StarterGear if the player doesn’t have the tool but nothing has been working.

ServerScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local function IsEquipped(player, tool)
	local character = player.Character

	if tool:IsA("Tool") and tool.Parent == character then
		return true
	end

	return false
end

local function GetTool(player, tool, folder)
	for i,v in ipairs(folder:GetChildren()) do
		if v:IsA("Tool") and v == tool then
			return v
		end
	end

	return nil
end

local function HasTool(player, tool)
	local StarterGear = player:WaitForChild("StarterGear")
	local Backpack = player:WaitForChild("Backpack")

	local hasToolInStarterGear = GetTool(player, tool, StarterGear)
	local hasToolInBackpack = GetTool(player, tool, Backpack)
	
	if hasToolInStarterGear or hasToolInBackpack then
		return true
	end
	
	return false
end

local function AddTool(player, tool)
	if tool:IsA("Tool") then
		local StarterGear = player:WaitForChild("StarterGear")
		local Backpack = player:WaitForChild("Backpack")
		
		local hasToolInStarterGear = GetTool(player, tool, StarterGear)
		--local isEquipped = IsEquipped(player, tool)
		
		if not hasToolInStarterGear then
			tool:Clone().Parent = StarterGear
			Remotes.UpdateBackpack:FireClient(player, tool, "Add")
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Backpack = player:WaitForChild("Backpack")
		local StarterGear = player:WaitForChild("StarterGear")

		for i,v in ipairs(Backpack:GetChildren()) do
			AddTool(player, v)
		end

		Backpack.ChildAdded:Connect(function(child)
			AddTool(player, child)
		end)

		Backpack.ChildRemoved:Connect(function(child)
			local result = IsEquipped(player, child)

			if not result then
				spawn(function()
					print("backpack removed")
					child:Destroy()
				end)
			end
		end)
		
		character.ChildAdded:Connect(function(child)
			AddTool(player, child)
		end)

		character.ChildRemoved:Connect(function(child)
			if child:IsA("Tool") then
				local isEquipped = IsEquipped(player, child)
				local hasTool = HasTool(player, child)

				if not isEquipped and not hasTool then
					print("character removed")
					Remotes.UpdateBackpack:FireClient(player, child, "Remove")
				end
			end
		end)
	end)
end)

1 Like

If you dont want the tool to duplicate itself, then why not just use roblox’s default backpack system?

I don’t really understand what you mean.

You want the tool to be saved in StarterGear and then parent it to the player?

StarterGear automatically gives the tools inside to the player.

Have you tried to use Server Storage for the tool instead?

Sorry if I didn’t understood what you meant.

Because their default backpack system doesn’t save the tool layout when the player resets.

So I decided to create my own backpack system.

place the items in server storage then clone them to the players backpack, if you have it do this every time the player dies they will keep there items.

I will try this after I get home from school.

I changed up my code but I keep running into the same problem. Everytime you equip a tool, it updates the backpack.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local function IsEquipped(player, tool)
	local character = player.Character

	if tool:IsA("Tool") and tool.Parent == character then
		return true
	end

	return false
end

local function GetTool(player, tool, folder)
	for i,v in ipairs(folder:GetChildren()) do
		if v:IsA("Tool") and v == tool then
			return v
		end
	end

	return nil
end

local function AddToBackpackFolder(player, tool)
	local BackpackFolder = player:WaitForChild("BackpackFolder")
	
	local ToolObject = Instance.new("ObjectValue")
	ToolObject.Name = tool.Name
	ToolObject.Value = tool
	ToolObject.Parent = BackpackFolder
end

local function RemoveToolFromBackpackFolder(player, tool)
	local BackpackFolder = player:WaitForChild("BackpackFolder")
	local getTool = GetTool(player, tool, BackpackFolder)
	
	if getTool then
		getTool:Destroy()
	end
end

local function AddTool(player, tool)
	if tool:IsA("Tool") then
		local BackpackFolder = player:WaitForChild("BackpackFolder")
		--local Backpack = player:WaitForChild("Backpack")
		
		local hasToolInBackpackFolder = GetTool(player, tool, BackpackFolder)
		local isEquipped = IsEquipped(player, tool)
		
		if not hasToolInBackpackFolder then
			AddToBackpackFolder(player, tool)
			Remotes.UpdateBackpack:FireClient(player, tool, "Add")
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local BackpackFolder = player:WaitForChild("BackpackFolder")
		local Backpack = player:WaitForChild("Backpack")

		for i,v in ipairs(Backpack:GetChildren()) do
			AddTool(player, v)
		end

		Backpack.ChildAdded:Connect(function(child)
			AddTool(player, child)
		end)

		Backpack.ChildRemoved:Connect(function(child)
			local result = IsEquipped(player, child)

			if not result then
				spawn(function()
					print("backpack removed")
					
					child:Destroy()
				end)
			end
		end)
		
		character.ChildAdded:Connect(function(child)
			print("tool added to character")
			
			AddTool(player, child)
		end)

		character.ChildRemoved:Connect(function(child)
			if child:IsA("Tool") then
				local isEquipped = IsEquipped(player, child)
				local hasToolInBackpackFolder = GetTool(player, child, BackpackFolder)

				if not isEquipped and not hasToolInBackpackFolder then
					print("character removed")
					
					RemoveToolFromBackpackFolder(player, child)
					Remotes.UpdateBackpack:FireClient(player, child, "Remove")
				end
			end
		end)
	end)
end)

in Backpack.ChildRemoved:Connect(function(child) you can check again if the part is in character then don’t give another tool, bc when you equipt the tool it will delete the tool in you backpack and clone it into you character and ChildRemoved event will fire too

you can make it like this

Backpack.ChildRemoved:Connect(function(child)
	if character:FindFirstChild(child.name) then
		print("ok don't remove")
	else
		local result = IsEquipped(player, child)

		if not result then
			spawn(function()
				print("backpack removed")
				child:Destroy()
			end)
		end
	end
end)

But when ChildRemoved is fired, the tool won’t be in the character anymore though.

so you just do the same thing to the checking, let it check if the part is not inside character then check inside the backpack