Efficient way of detecting when a new tool has been added to a player's backpack

Hello, I’m currently attempting to make a gun system that relies on a single module script to load the code for all the guns that are put into the player’s backpack. In an effort to try to keep the scripts to a minimum, the only script within the guns themselves is a module script that has the specific configuration for it. What I’m trying to achieve is that when a tool is added to a player’s backpack, it loads the code. I currently have a local script in StarterPlayerScripts that is going to handle loading the code for the guns, however when using the.ChildAdded event on the backpack folder, it fires every time you unequip the weapons and doesn’t fire when you first load in. I was wondering what the best way to check when a player has received a new item in their backpack would be.

TLDR: How should I go about detecting when a player gets a new item in their backpack without it firing when they unequip it, and firing when it’s first added?

3 Likes

You could make a variable equal to any number, then when the tool get’s added for the first time add +1 to the variable. Then whenever you use ChildAdded, check to make sure that the variable is less than a the starting value + 1. This will make it so that it would only fire the first time.

Thanks for the help, however this seems like a kind of, ‘hacky’ solution and isn’t really a very efficient or clean one. As well, the .ChildAdded event doesn’t fire on first load so :man_shrugging: :

1 Like