Child added function not firing

The title is very clear, my child added function is not firing in the backpack. Also, I tried enabling and disabling Backpack GUI, it doesn’t work.

child added func:

thanks! can provide more code if wanted

1 Like

Please share how children are added into the backpack and what environment this script is running in.

1 Like

the create tool function is being called for each item in the inventory table

Oh, also, the module script is in serverstorage, the childadded in InventoryGUI and other scripts are in serverscriptservice

1 Like

Please share how you defined the Backpack Variable.

1 Like

It happens because the backpack is recreated every time the character respawns.


https://developer.roblox.com/en-us/api-reference/class/Backpack

This was the best approach I’ve found:

local function SetupBackpack(backpack)
   backpack.ChildAdded:Connect(function(child)
      print('Child Added:', child)
   end)
end

local backpack = character:WaitForChild('Backpack')
SetupBackpack(backpack)

character.ChildAdded:Connect(function(child)
   if (child:IsA('Backpack')) then
      SetupBackpack(child)
   end
end)
1 Like

It’s not working even if I don’t reset, though

1 Like

I was trying to inquire whether OP followed this. However, thank you for sharing the information. :smile:

1 Like

Please share the rest of the script.

1 Like

That’s the entire script, pretty much

How do you define Backpack? Please share if any part of the script is left out, especially the declaration.


only the declarations were left out, here you go

Backpack is still not defined. Do you mind copy-pasting the entire script?

local player = game.Players.LocalPlayer

local InventoryGUI = player.PlayerGui.InventoryGUI
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Values = game.ServerStorage:WaitForChild("Tools")


local function SetupBackpack(backpack)
	print('setting up slots')
	backpack.ChildAdded:Connect(function(child)
		local toolAssociatedWithChild = Values:WaitForChild(child.Name)
		if toolAssociatedWithChild then
			local slots = InventoryGUI.Frame:GetChildren()
			for i, v in pairs(slots) do
				if v.Name == "empty" then
					if child.Name == "Fire" then
						print("Fire button is being created")
						v.Name = "Fire"
						v:WaitForChild("ImageButton").Image = "rbxasset://7342041017"
						v:WaitForChild("ImageButton").Visible = true
						print('done')
					end
				else
					print("Slot not empty!")
				end
			end
		end
	end)
end

local backpack = player.Character:WaitForChild('Backpack')
SetupBackpack(backpack)

player.Character.ChildAdded:Connect(function(child)
	if (child:IsA('Backpack')) then
		SetupBackpack(child)
	end
end)

--changed the script with @GAFFAL_1236's answer

Please ensure: the Reset on Spawn property of the InventoryGUI is not turned on. Please check and let me know.

Edit: Also, a local script cannot read from ServerStorage, consider moving “Values” to ReplicatedStorage.

It’s still not working, I’ve unchecked the reset on spawn property and moved the folder to replicatedstorage

Please share an the updated script and an image of your object hierarchy. Thank you.

Script

local player = game.Players.LocalPlayer

local InventoryGUI = player.PlayerGui.InventoryGUI
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Values = ReplicatedStorage:WaitForChild("Tools")


local function SetupBackpack(backpack)
	print('setting up slots')
	backpack.ChildAdded:Connect(function(child)
		local toolAssociatedWithChild = Values:WaitForChild(child.Name)
		if toolAssociatedWithChild then
			local slots = InventoryGUI.Frame:GetChildren()
			for i, v in pairs(slots) do
				if v.Name == "empty" then
					if child.Name == "Fire" then
						print("Fire button is being created")
						v.Name = "Fire"
						v:WaitForChild("ImageButton").Image = "rbxasset://7342041017"
						v:WaitForChild("ImageButton").Visible = true
						print('done')
					end
				else
					print("Slot not empty!")
				end
			end
		end
	end)
end

local backpack = player.Character:WaitForChild('Backpack')
SetupBackpack(backpack)

player.Character.ChildAdded:Connect(function(child)
	if (child:IsA('Backpack')) then
		SetupBackpack(child)
	end
end)


Object hierarchy

I might be saying wrong but isn’t the Backpack supposed to be inside the player rather than the character? If you added a backpack inside the character then alright…

Sorry for being late, but now it adds the same tool in 5 slots

edit: you solved it anyway

1 Like