Backpack.ChildAdded fires; child added is not added though

My story

I listen for the ChildAdded event on backpack for my prison game. If prisoners grab a weapon then it gets inserted to their backpack via Instance::Clone. I can then determine that they are no longer “innocent” by switching on a BoolValue’s Value property and thus they can be arrested. But the issue for me was that ChildAdded simply did not fire when the tool was cloned into their backpack.

The problem

I was wondering if it was just an issue with my implementation so I opened up an empty baseplate and tried reproducing it there. Funnily I got the opposite. Instead of ChildAdded not firing even though a tool was parented to the backpack, the event fired, but no tool was in the backpack so I couldn’t equip it.

Repro

Below is a repro file that will reproduce this issue:

repro_place.rbxl (18,6 KB)

The script also outputs to the console so you can see what I mean.


Through some research I was able to find this: Backpack.ChildAdded is not fired when tools are put into the backpack at CharacterAdded

But it is in #bulletin-board.

2 Likes

You’ll find that in all those cases that you’re out-of-sync with the current Backpack object–It gets replaced.
In your repro, you are doing it too early for it to reliably be the Backpack that the player ends up with.
If you do it in CharacterAdded for example, you’ll find that it will work.
Edit:
I did a little experiment w/ your repro, and in studio if I waited ~1s into the PlayerAdded listener,
then it was able to find the up-to-date Backpack.

1 Like

Hmm :confused:

I used Instance::WaitForChild so this couldn’t have been the issue. Apparently there is a new backpack everytime you respawn but I didn’t respawn so this couldn’t have been the issue. As I stated:

I printed the name of the tool on ChildAdded however it wasn’t in the backpack. I could be misunderstanding something here though

As I stated, it gets replaced–Even on an initial spawn. There is nothing wrong with the events.
Edit:
Maybe I should clarify: There are multiple Backpack objects at play, and when you Connect to a
Backpack Event, it can seem like events aren’t working because you’re assuming you’re still using the
Backpack you had prior, but in fact the Backpack is different, so your Connected Listeners won’t fire
because they’re on the wrong Backpack. And likewise adding to the Backpack–If you add to the previous
Backpack, your Tool won’t be in the new Backpack. So this is why you can get the “opposite” effect you
mentioned, eg. your initial add and listener are on a Backpack that no longer is the current Player backpack, that’s why the Tool is not in the Backpack later (So you got the initial fire, but if you add another tool later, to the proper current Backpack, you won’t, because the listener is on the wrong Backpack.)

4 Likes