Does anyone know about this. "TextButton" Template:Clone()

Please help
I am very confused about this. maybe someone knows how to fix it.
I’ve tried to fix it. but always wrong

Script

local InventoryGUI = script.Parent
local Main = InventoryGUI.Main
local FrameItems = Main.FrameItems
local Template = FrameItems.Template
Template.Parent = script


local Player = game.Players.LocalPlayer
local Inventory = Player:WaitForChild("Inventory")
local Backpack = Player.Backpack
local StarterGear = Player.StarterGear

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local InventoryEvent = ReplicatedStorage:WaitForChild("InventoryEvent")

local function clear()
	for i,v in pairs(FrameItems:GetChildren())do
		if v:IsA("Frame") then
			v:Destroy()
		end
	end
end


local function update()
	clear()
	
	local character = Player.Character or Player.CharacterAdded:Wait()

	for i,tool in pairs(Inventory:GetChildren())do
		
		if tool:IsA("Tool")then
			
			local check = Backpack:FindFirstChild(tool.Name) or character:FindFirstChild(tool.Name)
			
			local Items =  Template:Clone()
			Items.Parent = FrameItems
			Items.TextButton.Text = "Equip"
			Items.TextLabel.Text = tool.Name
			Items.ImageLabel.Image = tool.TextureId
			
			if check and Items then
				Items.TextButton.Text = "Unequip"
			end

			spawn(function()
				
				if Items then
					Items.TextButton.MouseButton1Click:Connect(function()


						if not check then
							InventoryEvent:FireServer(tool, "Equip")

						else
							InventoryEvent:FireServer(tool, "Unequip")
							
						end
						
						update()
						
					end)
				end
				
			end)
		end
	end
end
 
Backpack.ChildAdded:Connect(function()
	update()
end)
Backpack.ChildRemoved:Connect(function()
	update()
end)

StarterGear.ChildAdded:Connect(function()
	update()
end)
StarterGear.ChildRemoved:Connect(function()
	update()
end)

Inventory.ChildAdded:Connect(function()
	update()
end)
Inventory.ChildRemoved:Connect(function()
	update()
end)

update()

local InventoryGUI = script.Parent
local Main = InventoryGUI.Main
local FrameItems = Main.FrameItems
local Template = FrameItems.Template
Template.Parent = script
local Player = game.Players.LocalPlayer
local Inventory = Player:WaitForChild("Inventory")
local Backpack = Player.Backpack
local StarterGear = Player.StarterGear
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local InventoryEvent = ReplicatedStorage:WaitForChild("InventoryEvent")

local function clear()
	for i,v in pairs(FrameItems:GetChildren())do
		if v:IsA("Frame") then
			v:Destroy()
		end
	end
end

local function update()
	clear()
	local character = Player.Character or Player.CharacterAdded:Wait()
	for i,tool in pairs(Inventory:GetChildren())do
		if tool:IsA("Tool")then
			local check = Backpack:FindFirstChild(tool.Name) or character:FindFirstChild(tool.Name)
			local Items =  Template:Clone()
			Items.Parent = FrameItems
			for _, child in ipairs(Template:GetChildren()) do
				local clone = child:Clone()
				clone.Parent = Items
			end
			Items.TextButton.Text = "Equip"
			Items.TextLabel.Text = tool.Name
			Items.ImageLabel.Image = tool.TextureId
			if check and Items then
				Items.TextButton.Text = "Unequip"
			end
			spawn(function()
				if Items then
					Items.TextButton.MouseButton1Click:Connect(function()
						if not check then
							InventoryEvent:FireServer(tool, "Equip")
						else
							InventoryEvent:FireServer(tool, "Unequip")
						end
						update()
					end)
				end
			end)
		end
	end
end

Backpack.ChildAdded:Connect(function()
	update()
end)

Backpack.ChildRemoved:Connect(function()
	update()
end)

StarterGear.ChildAdded:Connect(function()
	update()
end)

StarterGear.ChildRemoved:Connect(function()
	update()
end)

Inventory.ChildAdded:Connect(function()
	update()
end)

Inventory.ChildRemoved:Connect(function()
	update()
end)

update()

If you are going to help, please let them know what you did to fix their code other than just posting a reformatted code with seemingly no change other than the spacing.


@OP
The error tells you that the TextButton is no longer a child of the Template which is stored in the Items variable. So sometime between the cloning and doing the spawn() function, the TextButton is getting removed somehow.

1 Like

It’s because the children aren’t being cloned with the parent (the template), so the change I added was too loop through the children of the template and explicitly clone those as well.

That or the template doesn’t contain those instances in the first place.

I did also fix the formatting as you stated.


Hmm

When you clone an object, it also copies its children. You would not have to clone each child object as well. But sometimes there could be a slight delay so using WaitForChild might be necessary in case a cloned child did not appear right away.

What I’d do is move the template stuff to a separate folder (not inside one another just inside a folder) and then clone the instances from there as necessary, for whatever reason sometimes when you’re cloning an instance the children of that instance aren’t being cloned with it.

1 Like

I’m aware of this convention but under certain circumstances :Clone() will fail to clone an instances children (if they have not yet loaded/replicated etc.).

Ok, thanks for the help. maybe i will try later.