Player.Backpack.ChildAdded not working

Hi guys, please look at this video and pay attention to “Connecting” which prints, but it looks like it connects .ChildAdded but it doesn’t fires when a child gets added to backpack


code;

print('Connecting!')
	
	Player.Backpack.ChildRemoved:Connect(function(child)
		print('a')
		local find = Player.Character:FindFirstChild(child.Name)
		if (find == nil) then
			print('a')
			for i,v in pairs(Backpack.Slots:GetChildren()) do
				if (v:IsA("Frame")) then
					if (v.TName.Text == child.Name) then
						print('b')
						v.BackgroundTransparency = .7;
						--v.Visible = false;
						v.TName.Text = ''
					end
				end
			end
		end;
	end)

	Player.Backpack.ChildAdded:Connect(function(child)
		print('aa')
		for i = 1,7 do
			local slot = Backpack.Slots:FindFirstChild(i)
			if slot then
				if (slot.Visible == false and #slot.TName.Text < 1) then
					local continue = true
					for i,v in pairs(Backpack.Slots:GetChildren()) do
						if (v:IsA("Frame")) then
							if (v.TName.Text == child.Name) then
								continue = false;
							end
						end
					end
					if (continue == true) then
						slot.TName.Text = child.Name;
						--slot.Visible = true;
					end
				end
			end
		end
	end)

It’s not printing a

1 Like

Try printing to see if the connection is connected in the first place. If it is then it may be an internal issue. Or it may be an issue on the client vs. server.

print('Connecting!')
	
	local removed = Player.Backpack.ChildRemoved:Connect(function(child)
		print('a')
		local find = Player.Character:FindFirstChild(child.Name)
		if (find == nil) then
			print('a')
			for i,v in pairs(Backpack.Slots:GetChildren()) do
				if (v:IsA("Frame")) then
					if (v.TName.Text == child.Name) then
						print('b')
						v.BackgroundTransparency = .7;
						--v.Visible = false;
						v.TName.Text = ''
					end
				end
			end
		end;
	end)

	local added = Player.Backpack.ChildAdded:Connect(function(child)
		print('aa')
		for i = 1,7 do
			local slot = Backpack.Slots:FindFirstChild(i)
			if slot then
				if (slot.Visible == false and #slot.TName.Text < 1) then
					local continue = true
					for i,v in pairs(Backpack.Slots:GetChildren()) do
						if (v:IsA("Frame")) then
							if (v.TName.Text == child.Name) then
								continue = false;
							end
						end
					end
					if (continue == true) then
						slot.TName.Text = child.Name;
						--slot.Visible = true;
					end
				end
			end
		end
	end)

print("Connected Remove:",removed.Connected)
print("Connected Added:",added.Connected)

Does it print true or false?

It’s indeed connected, so I don’t know what’s causing it.

I tested it on another game same code and works fine, on a baseplate, but in my game, it doesn’t prints ‘a’ so it’s not urnning the function :thinking:

Can anyone help?
I have no clue why it doesn’t works in baseplate but not in my game…

It doesn’t even makes sense.

Here you can see i’m literally CLONING the script and pasting it into the localscript that runs it (same interface and code), as you can see .ChildAdded works in baseplate but not in MY game… what the hell?

can someone tell me what’s wrong? It’s unbelievable.

There’s a silent bug in your event. They can be sneaky to debug, what I’d recommend doing is putting a breakpoint on the line (click next to the line number) where you print the connection in the event and step through it

If there are any issues with a conditional, stepping through it will make it instantly obvious which condition isn’t resolving as true, or if there is some other code misbehaving.

1 Like

Yeah I believe it may be client vs server as @BuilderBob25620 said.

Try switching to server then drag the tool to backpack.

ChildAdded should still fire here since the code is running from the client.

Already did, and get the same results adding/removing it from server.

Putted a breakpoint and this is what call stack returned;


Ok now press the button called Step Into under the Script tab to step through your code. Since the code is finishing (since there’s no errors), it’s better to visualise the computational flow of your program and not just what the functions do.

Also put it after the function(), statement, it’s currently breaking when you connect the function, not when it runs

Oh apologies, then I will suggest some more ideas.

Once a character dies, the Backpack is removed and a new one will be created – populating

Usually this gets messy in a real game with stuff like player:LoadCharacter() as I have experienced with other help topics especially with GUIs. Sometimes the backpack gets destroyed then readded which destroys the connection.

I would also maybe do this to debug further.

local Player = game.Players.LocalPlayer

Player.ChildAdded:Connect(function(child)

	if child.Name == "Backpack" then
		print(child, "New backpack added!")
	end

end)

ex: I click play and somehow there were two backpacks were created.

2 Likes

I clicked step into and my studio crashed, sort of weird…
also @dthecoolest I’ll try that.

As you can see here, Player.Backpack.ChildAdded not working - #3 by varjoyTes
It was indeed connected so if it was destroyed it wouldnt be connected right?

Hi, when I clicked on Step Into it went from


.ChildRemoved straight to :GetChildren(), I guess there is a problem there uh??!?

Well yes it would be connected.

However it could be connected to the old backpack which is now is nil after being destroyed.

It will still exist in the script because the prints are holding a reference and prevent it from being garbage collected. Same thing as doing

Part:Destroy()
print(Part)-- prints the part

The new backpack would then not be connected, check the print order as well perhaps.

I have tried also :GetFullName() and it prints the path to the player, but ok, I’ll give a try to your solution!

yes, it prints new backpack added

never happened this to me before, why does this happens tho?
and how can I fix it?