Backpack.ChildAdded not triggering after reset

Hello!

In short - I make my own toolbar with three slots for a specific tools. I’ll try to keep it as simple as possible.
The problem is that it works perfectly until I reset my character.
More specifically, “Backpack.ChildAdded” doesn’t work anymore after the reset.

Existing posts are not helpful. Also, this script is far from finished, and don’t ask me about strange solutions either, because I’m actually a modeler not a scripter. :sweat_smile:

Thanks!

local MainFrame = script.Parent
local Player = game.Players.LocalPlayer
local Backpack = Player.Backpack

local character = Player.Character
local humanoid = character:WaitForChild("Humanoid")

--Fix
local function FixSlots()
	for i,v in pairs(MainFrame:GetChildren()) do
		if v:IsA("TextButton") then
			if Backpack:FindFirstChild(tostring(v.ToolObject.Value)) then
				local Res = Backpack:FindFirstChild(tostring(v.ToolObject.Value))
				v.ToolObject.Value = Res
			end
		end
	end
end

Player.CharacterAdded:Connect(function()
character = Player.Character
humanoid = character:WaitForChild("Humanoid")	
Player = game.Players.LocalPlayer
Backpack = Player:WaitForChild("Backpack")
MainFrame = script.Parent
	FixSlots()
print("Current BP Parent:".. Backpack.Parent.Name )	
end)	


Backpack.ChildAdded:Connect(function(LastAdded) print("ChildAdded")
	if MainFrame.SpecialToolFrame.ToolObject.Value ~= LastAdded and MainFrame.BottleFrame1.ToolObject.Value ~= LastAdded and MainFrame.BottleFrame2.ToolObject.Value ~= LastAdded then
	if LastAdded:GetAttribute("Special") == true then
		MainFrame.SpecialToolFrame.ToolObject.Value = LastAdded
		MainFrame.SpecialToolFrame.Text = LastAdded.Name
	elseif LastAdded:GetAttribute("Special") == false then
			if MainFrame.BottleFrame1.ToolObject.Value == nil then
			MainFrame.BottleFrame1.ToolObject.Value = LastAdded
			MainFrame.BottleFrame1.Text = LastAdded.Name
		else
			MainFrame.BottleFrame2.ToolObject.Value = LastAdded
			MainFrame.BottleFrame2.Text = LastAdded.Name
		end
		end
	end
end)

local function CleanRemoved(LastRemoved)
if LastRemoved.Parent ~= character then	
	for i,v in pairs(MainFrame:GetChildren()) do
			if v:IsA("TextButton") then
				if v.ToolObject.Value == LastRemoved then
					v.ToolObject.Value = nil
					v.Text = "+"
			end
		end
	end
end
end

Backpack.ChildRemoved:Connect(function(LastRemoved)
	CleanRemoved(LastRemoved)
end)
--[[
character.ChildRemoved:Connect(function(LastRemoved)
	if LastRemoved:IsA("Tool") then
		CleanRemoved(LastRemoved)
	end
	end)
]]
Player.Character.ChildRemoved:Connect(function(LastRemovedIC)
	if LastRemovedIC:IsA("Tool") then
		if not Backpack:FindFirstChild(LastRemovedIC) then
			for i,v in pairs(MainFrame:GetChildren()) do
				if v:IsA("TextButton") then
					if v.ToolObject.Value == LastRemovedIC then
						v.ToolObject.Value = nil
						v.Text = "+"
					end
				end
			end
		end
	end
end)


local function UnEquipTools()
	for i,v in pairs(character:GetChildren()) do
		if v:IsA("Tool") then
			v.Parent = Backpack
		end
	end
end


local function EquipTool(number)
	--1
	if number == 1 then
		if MainFrame.SpecialToolFrame.ToolObject.Value ~= nil then
			if MainFrame.SpecialToolFrame.ToolObject.Value.Parent == Backpack then
				UnEquipTools()
				MainFrame.SpecialToolFrame.ToolObject.Value.Parent = character
			elseif MainFrame.SpecialToolFrame.ToolObject.Value.Parent == character then
				MainFrame.SpecialToolFrame.ToolObject.Value.Parent = Backpack
				UnEquipTools()
			end
		end
	--2
	elseif number == 2 then
		if MainFrame.BottleFrame1.ToolObject.Value ~= nil then
			if MainFrame.BottleFrame1.ToolObject.Value.Parent == Backpack then
				UnEquipTools()
				MainFrame.BottleFrame1.ToolObject.Value.Parent = character
			elseif MainFrame.BottleFrame1.ToolObject.Value.Parent == character then
				MainFrame.BottleFrame1.ToolObject.Value.Parent = Backpack
				UnEquipTools()
			end
		end
	--3	
	elseif number == 3 then
		if MainFrame.BottleFrame2.ToolObject.Value ~= nil then
			if MainFrame.BottleFrame2.ToolObject.Value.Parent == Backpack then
				UnEquipTools()
				MainFrame.BottleFrame2.ToolObject.Value.Parent = character
			elseif MainFrame.BottleFrame2.ToolObject.Value.Parent == character then
				MainFrame.BottleFrame2.ToolObject.Value.Parent = Backpack
				UnEquipTools()
			end
		end
	end
end



local UIP = game:GetService("UserInputService")

UIP.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.One then
		EquipTool(1)
	elseif key.KeyCode == Enum.KeyCode.Two then print("Key 2 pressed")
		EquipTool(2)
	elseif key.KeyCode == Enum.KeyCode.Three then
		EquipTool(3)
	end
end)

You will need to:

  1. Change the Backpack variable every time the character resets (which is already implemented). not needed, you can just connect the events from the CharacterAdded function…
  2. You should store you Backpack.ChildAdded and Backpack.ChildRemoved connections into a table instead of connecting it straight (this is to prevent the memory leaks on the next set connections after reset)
  3. In the CharacterAdded event’s anonymous function, disconnect the old Backpack connections that we’d stored earlier and then connect it to the newer Backpack.

If you need a code example, just tell me…

Thanks for responding! I would also like an code example.

Sure, here is a code example of how to store and reuse connections:

local Player = game:GetService("Players").LocalPlayer
local Connections = {} :: {[string]: RBXScriptConnection}  -- Here where all the disposal Connections would be.

local function DisconnectConnections()
    for ConnectionName, Connection in pairs(Connections) do
        if Connection.Connected then
            Connection:Disconnect()
        end
    end
    Connections = {}
end

local function BackpackChildAdded(Child: Instance)
    -- ...
end

local function BackpackChildRemoved(Child: Instance)
    -- ...
end

local function CharacterAdded(Character)
    if not Character then
        return
    end
    DisconnectConnections()
    local Backpack = Player:WaitForChild("Backpack")
    Connections.BackpackChildAdded = Backpack.ChildAdded:Connect(BackpackChildAdded)
    Connections.BackpackChildRemoved = Backpack.ChildRemoved:Connect(BackpackChildRemoved)
    Connections.CharacterChildAdded = -- ...
    Connections.CharacterChildRemoved = -- ...
end

Player.CharacterAdded:Connect(CharacterAdded)
CharacterAdded(Player.Character)    -- The player character could be already spawned before the connection
1 Like

Excellent! It works perfectly now.
Thanks!

1 Like

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