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

I’ve made a script that detects any tool added to player’s backpack and will run some code only for the items that have not been added to that player’s backpack before.

function onChildAdded(child,plr)
	if not plr then return end
	if not child:HasTag(tostring(plr).."'s tool") then
		child:AddTag(tostring(plr).."'s tool")
		-- Your code here that needs to run for the new items only
	end
end

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local backpack = plr.Backpack
	local connection = backpack.ChildAdded:Connect(function(child) onChildAdded(child,plr) end)
	plr.CharacterAdded:Connect(function()
		backpack = plr.Backpack -- Due to roblox limitations we have to reassign the backpack on respawn
		connection:Disconnect() -- Remove previous connections so we don't have multiple functions trying to run
		connection = backpack.ChildAdded:Connect(function(child) onChildAdded(child,plr) end)
	end)
end)

I had to make a separate connection to the Backpack.ChildAdded event, since you have to reassign the backpack each time the player respawns for ChildAdded to work. To detect “old” items I used tags system, which adds a tag to any new tool named “'s tool”, so the required code won’t run for items that have a tag like this, aka “old” items.

Maybe have the script that adds the tool fire a sort of BindableEvent with an argument leading to the tool in the backpack, then use another script or a function in the module script to load the weapon’s code.