Backpack Detection not working

No clue why it doesn’t work and I’m probably just dumb lol.

Any help? I want it so if you have above ‘max’ tools in your backpack, it gets removed.

local Players = game:GetService("Players")
local Max = 3

Players.PlayerAdded:Connect(function(Player)
	local Backpack = Player.Backpack
	local Current
	
	Backpack.ChildAdded:Connect(function(Child)
		print(Child.Name)
		print("fired becaues yes")
		Current = Backpack:GetChildren()
		print(Current)
		if Current >= Max then
			Child:Destroy()
		end
	end)
end)
local Players = game:GetService("Players")
local Max = 3

Players.PlayerAdded:Connect(function(Player)
	local Backpack = Player.Backpack
	local Current
	
	Backpack.ChildAdded:Connect(function(Child)
		print(Child.Name)
		print("fired becaues yes")
		Current = Backpack:GetChildren()
		print(Current) --theres no hashtag so it wont recognise it as a number
		if Current >= Max then
			Child:Destroy()
		end
	end)
end)

Instead, do this:

local Players = game:GetService("Players")
local Max = 3

Players.PlayerAdded:Connect(function(Player)
	local Backpack = Player.Backpack
	local Current
	
	Backpack.ChildAdded:Connect(function(Child)
		print(Child.Name)
		print("fired becaues yes")
		Current = Backpack:GetChildren()
		print(#Current)
		if #Current >= Max then
			Child:Destroy()
		end
	end)
end)

As you can see here, every usage of Current but the variable is now with a hashtag so it’s actually a number.

1 Like

I tried your code out and it still doesn’t work. The thing is that it doesn’t fire the ChildAdded function which means it’ll never execute the rest of the code inside that function. Maybe it doesn’t work in a PlayerAdded function?

Try this

local Players = game:GetService("Players")
local Max = 3

Players.PlayerAdded:Connect(function(Player)
	local Backpack = Player.Backpack
	local Current
	
	Backpack.ChildAdded:Connect(function(Child)
		print(Child.Name)
		print("fired becaues yes")
		Current = Backpack:GetChildren()
		print(Current)
		if Current >= Max then
			return
		end
	end)
end)

I changed child:Destroy() to return

How would that work? All I need is the child to get destroyed if it’s going over the maximum limit

When your character loads in, a new backpack instance is created. Meaning any references to old backpacks are not active.

local Players = game:GetService("Players")
local Max = 3

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(char)
		
		local Backpack = Player:WaitForChild("Backpack");
		local Current
	
		Backpack.ChildAdded:Connect(function(Child)
			print(Child.Name)
			print("fired becaues yes")
			Current = Backpack:GetChildren()
			print(Current)
			if Current >= Max then
				Child:Destroy()
			end
		end)
	end)
end)
1 Like

For some reason it doesn’t work. The previous error was ‘trying to set parent to null’ or something and ‘current parrent = backpack’, something like that.

Any issues?

local Players = game:GetService("Players")
local Max = 3

Players.PlayerAdded:Connect(
	function(Player)
		Player.CharacterAdded:Connect(
			function(char)
				local Backpack = Player:WaitForChild("Backpack")
				local Current

				Backpack.ChildAdded:Connect(
					function(Child)
						print(Child.Name)
						if Child:IsA("Tool") then
							local tool = Child
							Current = Backpack:GetChildren()
							if #Current >= Max then
								if Backpack:FindFirstChild(tool) then
									tool:Destroy()
								end
							end
						end
					end
				)
			end
		)
	end
)