Backpack ChildAdded Event is not firing?

I’m creating an inventory system which detects when a child is added to the players backpack, then fires a remote event to the client to add the icon to a gui. However, the child added event is not sending any remote events because no children are being added. I have 1 gear in the starter pack but I tested by adding something through the server side in game, which did not work either. Any ideas?

game.Players.PlayerAdded:Connect(function(player)
	local weaponFolder = Instance.new("Folder", player)
	weaponFolder.Name = "Weapons"
	
	local playerValueFolder = Instance.new("Folder", player)
	playerValueFolder.Name = "PlayerValues"
	
	local teleportingAntiSpam = Instance.new("BoolValue", player)
	teleportingAntiSpam.Name = "TeleportingAntiSpam"
	
	local playerBackpackValue = Instance.new("StringValue", player)
	playerBackpackValue.Name = "PlayerBackpack"
	
	player.CharacterAdded:Wait()
	character = player.Character
	local backpack = player:WaitForChild("Backpack")
	
	loadData(player)
	
	--If player is new
	if not player.PlayerValues:FindFirstChild("Money") then
		print("here")
		local money = Instance.new("NumberValue",playerValueFolder)
		money.Name = "Money"

		local lockpicks = Instance.new("NumberValue",playerValueFolder)
		lockpicks.Name = "Lockpicks"
	end

	
	backpack.ChildAdded:Connect(function(item)
		print(item)
		if item:FindFirstChild("Rarity") and game.ReplicatedStorage.Items:FindFirstChild(item.Rarity.Value) then
			if game.ReplicatedStorage.Items:FindFirstChild(item.Rarity.Value):FindFirstChild(item.Name) then
				game.ReplicatedStorage.Items.Events.ChildAddedEvent:FireClient(player, item)
			end
		end
	end)
end)

1 Like

It seems that the issue may be with the condition used to check if the item being added has the “Rarity” child and if it exists in “game.ReplicatedStorage.Items”.

Try checking if the “item” parameter is not nil before checking its children. Also, when using “game.ReplicatedStorage.Items:FindFirstChild()” you need to pass the name of the child you are looking for as a string.

backpack.ChildAdded:Connect(function(item)
	if item and item:FindFirstChild("Rarity") and game.ReplicatedStorage.Items:FindFirstChild(item.Rarity.Value) then
		local rarityFolder = game.ReplicatedStorage.Items[item.Rarity.Value]
		if rarityFolder and rarityFolder:FindFirstChild(item.Name) then
			game.ReplicatedStorage.Items.Events.ChildAddedEvent:FireClient(player, item)
		end
	end
end)

Also, check if the “PlayerBackpack” StringValue is being set when an item is added to the backpack. If it’s not being set properly, the client won’t know the backpack value and the remote event won’t be triggered.

It is not the rarity as the print(item) line does not even trigger, suggesting that the event isn’t even fired. I re-tested and it seems that it works when I manually add something after everything is loaded in. Maybe since it’s in starterpack the item loads in faster then the event can trigger?

Yes, everything in the StarterPack will not be noticed by the event. You need to manually add it or sort through the player’s inventory

Actually, all the tools are inside StarterPack. StarterPack is something like a default pack. Nothing inside it is observed by any event. Also, before all your code runs, StarterPack is too faster and it literally adds all the items into the Backpack after milliseconds of player joining.
So, when you waited for character to join and assigned backpack, items were added much before. So no event runs.

That’s the whole logic. Hope you understand.

Backpack is refreshed everytime character is spawned, you need to define it under a characteradded connection.

2 Likes

Umm… Doesn’t CharacterAdded event fires whenever character is added? Is it only for the first time?

No it fires everytime character is respawned.

Hi UmmmOkayWhat, it seems that the issue might be with the backpack.ChildAdded event not being triggered. The script should listen for changes on the client-side where the backpack changes are being made, so you may want to move the script to a LocalScript object instead of a regular Script object. You can also try adding some print statements to debug the code and make sure that the item variable is correctly referencing the child added to the backpack. Here’s the updated code:

	local weaponFolder = Instance.new("Folder", player)
	weaponFolder.Name = "Weapons"
	
	local playerValueFolder = Instance.new("Folder", player)
	playerValueFolder.Name = "PlayerValues"
	
	local teleportingAntiSpam = Instance.new("BoolValue", player)
	teleportingAntiSpam.Name = "TeleportingAntiSpam"
	
	local playerBackpackValue = Instance.new("StringValue", player)
	playerBackpackValue.Name = "PlayerBackpack"
	
	player.CharacterAdded:Wait()
	local character = player.Character
	local backpack = player:WaitForChild("Backpack")
	
	loadData(player)
	
	--If player is new
	if not player.PlayerValues:FindFirstChild("Money") then
		print("here")
		local money = Instance.new("NumberValue",playerValueFolder)
		money.Name = "Money"

		local lockpicks = Instance.new("NumberValue",playerValueFolder)
		lockpicks.Name = "Lockpicks"
	end

	-- Check for existing items in backpack
	for _, item in ipairs(backpack:GetChildren()) do
		if item:FindFirstChild("Rarity") and game.ReplicatedStorage.Items:FindFirstChild(item.Rarity.Value) then
			if game.ReplicatedStorage.Items:FindFirstChild(item.Rarity.Value):FindFirstChild(item.Name) then
				game.ReplicatedStorage.Items.Events.ChildAddedEvent:FireClient(player, item)
			end
		end
	end
	
	-- Listen for new items added to backpack
	backpack.ChildAdded:Connect(function(item)
		print(item)
		if item:FindFirstChild("Rarity") and game.ReplicatedStorage.Items:FindFirstChild(item.Rarity.Value) then
			if game.ReplicatedStorage.Items:FindFirstChild(item.Rarity.Value):FindFirstChild(item.Name) then
				game.ReplicatedStorage.Items.Events.ChildAddedEvent:FireClient(player, item)
			end
		end
	end)
end)

I hope this helps!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.