I can't find the error of my inventory script

Hey guys,
today I wrote an entire script to make my own Inventory.

I made 3 functions, one for hats, one for tools and one for pets.

What they do is delete all actual templates (Like item slot) and then create one template for each items in the right category (hats or pets or tools).

The player inventory is a folder and inside of it there is 3 folders : “PetsInventory”, “Tools Inventory” and “HatsInventory”.

A lot of thing are working, I can open the inventory, open a menu to equip it, but the script doesn’t delete the templates when we change the inventory category when the player close the inventory…

I’m trying so hard for 5 hours but I can’t find where is the problem…

This is my script : (Sorry it is very long and I made it by my self, I don’t think it is the best way to do an Inventory)

local Inventory = script.Parent
local frame = Inventory:WaitForChild("Frame")
local iFrame = frame:WaitForChild("IFrame")
local SFrame = frame:WaitForChild("ScrollingFrame")
local background = frame:WaitForChild("Background")
local template = SFrame:WaitForChild("Template")
local descPart = iFrame:WaitForChild("DescriptionBox")
local DisplayItem = iFrame:WaitForChild("DisplayItem")
local ItemName  = iFrame:WaitForChild("ItemName")
local status = Inventory:WaitForChild("Status")
local open = iFrame:WaitForChild("Open")
local module = require(game.StarterGui.GuiHolder)
local equipButton = iFrame:WaitForChild("EquipButton")
---- Onglets ----
local TOnglet = frame:WaitForChild("Tools-Icon")
local HOnglet = frame:WaitForChild("Hats-Icon")
local POnglet = frame:WaitForChild("Pets-Icon")

---- Define functions ---

local function toolDisplay()
	for _, temp in pairs(SFrame:GetChildren()) do
		if temp:IsA("TextButton") and not temp.Name == "Template" then
			temp:Destroy()
		end

		local plr = game.Players.LocalPlayer
		for _, tool in pairs(plr.Inventory.ToolsInventory:GetChildren()) do
			local newTemplate = template:Clone()
			newTemplate.Name = tool.Name
			newTemplate.ObjectName.Text = tool.Name
			newTemplate.Visible = true
			newTemplate.Parent = SFrame
			
			local object = tool:Clone()
			object.Parent = newTemplate.ViewportFrame
			
			local camera = Instance.new("Camera")
			camera.CFrame = CFrame.new(object.Handle.Position + (object.Handle.CFrame.lookVector*5),object.Handle.Position)
			camera.Parent = newTemplate.ViewportFrame
			
			newTemplate.ViewportFrame.CurrentCamera = camera
			
			newTemplate.MouseButton1Click:Connect(function()
				if open then
					if open.Value == false then
						descPart.Text = tool:FindFirstChild("Description").Value
						ItemName.Text = tool.Name

						local rarity = tool:FindFirstChild("Rarity").Value
						if rarity == "Common" then
							ItemName.TextColor3 = Color3.fromRGB(56, 56, 56)
						elseif rarity == "Uncommon" then
							ItemName.TextColor3 = Color3.fromRGB(95, 180, 76)
						elseif rarity == "Rare" then
							ItemName.TextColor3 = Color3.fromRGB(18, 59, 239)
						elseif rarity == "Epic" then
							ItemName.TextColor3 = Color3.fromRGB(126, 0, 189)
						elseif rarity == "Typical" then
							ItemName.TextColor3 = Color3.fromRGB(37, 185, 175)
						elseif rarity == "Legendary" then
							ItemName.TextColor3 = Color3.fromRGB(225, 49, 25)
						elseif rarity == "Mythic" then
							ItemName.TextColor3 = Color3.fromRGB(19, 255, 3)
						elseif rarity == "Special" then
							ItemName.TextColor3 = Color3.fromRGB(22, 2, 83)
						else
							---- There was an error ----
							ItemName.Text = "There was an error"
						end
						for _, v in pairs(DisplayItem.ViewportFrame:GetChildren()) do
							if v then
								v:Destroy()
							end
						end
						
						local object2 = tool:Clone()
						object2.Parent = DisplayItem.ViewportFrame
						
						local camera = Instance.new("Camera")
						camera.CFrame = CFrame.new(object2.Handle.Position + (object2.Handle.CFrame.lookVector*5),object2.Handle.Position)
						camera.Parent = DisplayItem.ViewportFrame
						
						DisplayItem.ViewportFrame.CurrentCamera = camera
						
						if plr.Character:FindFirstChild(tool.Name) then
							equipButton.Text = "Equipped"
						else
							equipButton.Text = "Equip"
						end
						
						local debounce = false
						equipButton.MouseButton1Click:Connect(function()
							if debounce == false then
								debounce = true
								local result = game.ReplicatedStorage.Remotes.EquipTool:InvokeServer(tool.Name)
								
								if result == true then
									equipButton.Text = "Equipped"
								end
							end
						end)
						module.openIFrame()
						open.Value = true
					else
						module.closeIFrame()
						descPart.Text = tool:FindFirstChild("Description").Value
						ItemName.Text = tool.Name

						local rarity = tool:FindFirstChild("Rarity").Value
						if rarity == "Common" then
							ItemName.TextColor3 = Color3.fromRGB(56, 56, 56)
						elseif rarity == "Uncommon" then
							ItemName.TextColor3 = Color3.fromRGB(95, 180, 76)
						elseif rarity == "Rare" then
							ItemName.TextColor3 = Color3.fromRGB(18, 59, 239)
						elseif rarity == "Epic" then
							ItemName.TextColor3 = Color3.fromRGB(126, 0, 189)
						elseif rarity == "Typical" then
							ItemName.TextColor3 = Color3.fromRGB(37, 185, 175)
						elseif rarity == "Legendary" then
							ItemName.TextColor3 = Color3.fromRGB(225, 49, 25)
						elseif rarity == "Mythic" then
							ItemName.TextColor3 = Color3.fromRGB(19, 255, 3)
						elseif rarity == "Special" then
							ItemName.TextColor3 = Color3.fromRGB(22, 2, 83)
						else
							---- There was an error ----
							ItemName.Text = "There was an error"
						end
						for _, v in pairs(DisplayItem.ViewportFrame:GetChildren()) do
							if v then
								v:Destroy()
							end
						end
						
						local object2 = tool:Clone()
						object2.Parent = DisplayItem.ViewportFrame
						
						local camera = Instance.new("Camera")
						camera.CFrame = CFrame.new(object2.Handle.Position + (object2.Handle.CFrame.lookVector*5),object2.Handle.Position)
						camera.Parent = DisplayItem.ViewportFrame
						
						DisplayItem.ViewportFrame.CurrentCamera = camera
						
						if plr.Character:FindFirstChild(tool.Name) then
							equipButton.Text = "Equipped"
						else
							equipButton.Text = "Equip"
						end
						
						local debounce = false
						equipButton.MouseButton1Click:Connect(function()
							if debounce == false then
								debounce = true
								local result = game.ReplicatedStorage.Remotes.EquipTool:InvokeServer(tool.Name)
								
								if result == true then
									equipButton.Text = "Equipped"
								end
							end
						end)
						wait(0.5)
						module.openIFrame()
					end
				end
			end)
		end
	end
end

local function hatDisplay()
	for _, temp in pairs(SFrame:GetChildren()) do
		if temp:IsA("TextButton") and not temp.Name == "Template" then
			temp:Destroy()
		end

		local plr = game.Players.LocalPlayer
		for _, hat in pairs(plr.Inventory.HatsInventory:GetChildren()) do
			local newTemplate = template:Clone()
			newTemplate.Name = hat.Name
			newTemplate.ObjectName.Text = hat.Name
			newTemplate.Visible = true
			newTemplate.Parent = SFrame
			
			local object = hat:Clone()
			object.Parent = newTemplate.ViewportFrame
			
			local camera = Instance.new("Camera")
			camera.CFrame = CFrame.new(object.Handle.Position + (object.Handle.CFrame.lookVector*5),object.Handle.Position)
			camera.Parent = newTemplate.ViewportFrame
			
			newTemplate.ViewportFrame.CurrentCamera = camera
			
			newTemplate.MouseButton1Click:Connect(function()
				if open then
					if open.Value == false then
						descPart.Text = hat:FindFirstChild("Description").Value
						ItemName.Text = hat.Name

						local rarity = hat:FindFirstChild("Rarity").Value
						if rarity == "Common" then
							ItemName.TextColor3 = Color3.fromRGB(56, 56, 56)
						elseif rarity == "Uncommon" then
							ItemName.TextColor3 = Color3.fromRGB(95, 180, 76)
						elseif rarity == "Rare" then
							ItemName.TextColor3 = Color3.fromRGB(18, 59, 239)
						elseif rarity == "Epic" then
							ItemName.TextColor3 = Color3.fromRGB(126, 0, 189)
						elseif rarity == "Typical" then
							ItemName.TextColor3 = Color3.fromRGB(37, 185, 175)
						elseif rarity == "Legendary" then
							ItemName.TextColor3 = Color3.fromRGB(225, 49, 25)
						elseif rarity == "Mythic" then
							ItemName.TextColor3 = Color3.fromRGB(19, 255, 3)
						elseif rarity == "Special" then
							ItemName.TextColor3 = Color3.fromRGB(22, 2, 83)
						else
							---- There was an error ----
							ItemName.Text = "There was an error"
						end
						for _, v in pairs(DisplayItem.ViewportFrame:GetChildren()) do
							if v then
								v:Destroy()
							end
						end
						
						local object2 = hat:Clone()
						object2.Parent = DisplayItem.ViewportFrame
						
						local camera = Instance.new("Camera")
						camera.CFrame = CFrame.new(object2.Handle.Position + (object2.Handle.CFrame.lookVector*5),object2.Handle.Position)
						camera.Parent = DisplayItem.ViewportFrame
						
						DisplayItem.ViewportFrame.CurrentCamera = camera
						
						if plr.Character:FindFirstChild(hat.Name) then
							equipButton.Text = "Equipped"
						else
							equipButton.Text = "Equip"
						end
						
						local debounce = false
						equipButton.MouseButton1Click:Connect(function()
							if debounce == false then
								debounce = true
								local result = game.ReplicatedStorage.Remotes.EquipTool:InvokeServer(hat.Name)
								
								if result == true then
									equipButton.Text = "Equipped"
								end
							end
						end)
						module.openIFrame()
						module.openIFrame()
						open.Value = true
					elseif open.Value == true then
						module.closeIFrame()
						wait(1.5)
						descPart.Text = hat:FindFirstChild("Description").Value
						ItemName.Text = hat.Name

						local rarity = hat:FindFirstChild("Rarity").Value
						if rarity == "Common" then
							ItemName.TextColor3 = Color3.fromRGB(56, 56, 56)
						elseif rarity == "Uncommon" then
							ItemName.TextColor3 = Color3.fromRGB(95, 180, 76)
						elseif rarity == "Rare" then
							ItemName.TextColor3 = Color3.fromRGB(18, 59, 239)
						elseif rarity == "Epic" then
							ItemName.TextColor3 = Color3.fromRGB(126, 0, 189)
						elseif rarity == "Typical" then
							ItemName.TextColor3 = Color3.fromRGB(37, 185, 175)
						elseif rarity == "Legendary" then
							ItemName.TextColor3 = Color3.fromRGB(225, 49, 25)
						elseif rarity == "Mythic" then
							ItemName.TextColor3 = Color3.fromRGB(19, 255, 3)
						elseif rarity == "Special" then
							ItemName.TextColor3 = Color3.fromRGB(22, 2, 83)
						else
							---- There was an error ----
							ItemName.Text = "There was an error"
						end
						for _, v in pairs(DisplayItem.ViewportFrame:GetChildren()) do
							if v then
								v:Destroy()
							end
						end
						
						local object2 = hat:Clone()
						object2.Parent = DisplayItem.ViewportFrame
						
						local camera = Instance.new("Camera")
						camera.CFrame = CFrame.new(object2.Handle.Position + (object2.Handle.CFrame.lookVector*5),object2.Handle.Position)
						camera.Parent = DisplayItem.ViewportFrame
						
						DisplayItem.ViewportFrame.CurrentCamera = camera
						
						if plr.Character:FindFirstChild(hat.Name) then
							equipButton.Text = "Equipped"
						else
							equipButton.Text = "Equip"
						end
						
						local debounce = false
						equipButton.MouseButton1Click:Connect(function()
							if debounce == false then
								debounce = true
								local result = game.ReplicatedStorage.Remotes.EquipTool:InvokeServer(hat.Name)
								
								if result == true then
									equipButton.Text = "Equipped"
								end
							end
						end)
						module.openIFrame()
					end
				end
			end)
		end
	end
end

local function petDisplay()
	for _, temp in pairs(SFrame:GetChildren()) do
		if temp:IsA("TextButton") and not temp.Name == "Template" then
			temp:Destroy()
		end

		local plr = game.Players.LocalPlayer
		for _, pet in pairs(plr.Inventory.PetsInventory:GetChildren()) do
			local newTemplate = template:Clone()
			newTemplate.Name = pet.Name
			newTemplate.ObjectName.Text = pet.Name
			newTemplate.Visible = true
			newTemplate.Parent = SFrame
			
			local object = pet:Clone()
			object.Parent = newTemplate.ViewportFrame
			
			local camera = Instance.new("Camera")
			camera.CFrame = CFrame.new(object.Part.Position + (object.Part.CFrame.lookVector*5),object.Part.Position)
			camera.Parent = newTemplate.ViewportFrame
			
			newTemplate.ViewportFrame.CurrentCamera = camera
			
			newTemplate.MouseButton1Click:Connect(function()
				if open then
					if open.Value == false then
						descPart.Text = pet:FindFirstChild("Description").Value
						ItemName.Text = pet.Name

						local rarity = pet:FindFirstChild("Rarity").Value
						if rarity == "Common" then
							ItemName.TextColor3 = Color3.fromRGB(56, 56, 56)
						elseif rarity == "Uncommon" then
							ItemName.TextColor3 = Color3.fromRGB(95, 180, 76)
						elseif rarity == "Rare" then
							ItemName.TextColor3 = Color3.fromRGB(18, 59, 239)
						elseif rarity == "Epic" then
							ItemName.TextColor3 = Color3.fromRGB(126, 0, 189)
						elseif rarity == "Typical" then
							ItemName.TextColor3 = Color3.fromRGB(37, 185, 175)
						elseif rarity == "Legendary" then
							ItemName.TextColor3 = Color3.fromRGB(225, 49, 25)
						elseif rarity == "Mythic" then
							ItemName.TextColor3 = Color3.fromRGB(19, 255, 3)
						elseif rarity == "Special" then
							ItemName.TextColor3 = Color3.fromRGB(22, 2, 83)
						else
							---- There was an error ----
							ItemName.Text = "There was an error"
						end
						for _, v in pairs(DisplayItem.ViewportFrame:GetChildren()) do
							if v then
								v:Destroy()
							end
						end
						
						local object2 = pet:Clone()
						object2.Parent = DisplayItem.ViewportFrame
						
						local camera = Instance.new("Camera")
						camera.CFrame = CFrame.new(object2.Handle.Position + (object2.Handle.CFrame.lookVector*5),object2.Handle.Position)
						camera.Parent = DisplayItem.ViewportFrame
						
						DisplayItem.ViewportFrame.CurrentCamera = camera
						
						if plr.Character:FindFirstChild(pet.Name) then
							equipButton.Text = "Equipped"
						else
							equipButton.Text = "Equip"
						end
						
						local debounce = false
						equipButton.MouseButton1Click:Connect(function()
							if debounce == false then
								debounce = true
								local result = game.ReplicatedStorage.Remotes.EquipTool:InvokeServer(pet.Name)
								
								if result == true then
									equipButton.Text = "Equipped"
								end
							end
						end)
						module.openIFrame()
						module.openIFrame()
						open.Value = true
					elseif open.Value == true then
						module.closeIFrame()
						wait()
						descPart.Text = pet:FindFirstChild("Description").Value
						ItemName.Text = pet.Name

						local rarity = pet:FindFirstChild("Rarity").Value
						if rarity == "Common" then
							ItemName.TextColor3 = Color3.fromRGB(56, 56, 56)
						elseif rarity == "Uncommon" then
							ItemName.TextColor3 = Color3.fromRGB(95, 180, 76)
						elseif rarity == "Rare" then
							ItemName.TextColor3 = Color3.fromRGB(18, 59, 239)
						elseif rarity == "Epic" then
							ItemName.TextColor3 = Color3.fromRGB(126, 0, 189)
						elseif rarity == "Typical" then
							ItemName.TextColor3 = Color3.fromRGB(37, 185, 175)
						elseif rarity == "Legendary" then
							ItemName.TextColor3 = Color3.fromRGB(225, 49, 25)
						elseif rarity == "Mythic" then
							ItemName.TextColor3 = Color3.fromRGB(19, 255, 3)
						elseif rarity == "Special" then
							ItemName.TextColor3 = Color3.fromRGB(22, 2, 83)
						else
							---- There was an error ----
							ItemName.Text = "There was an error"
						end
						for _, v in pairs(DisplayItem.ViewportFrame:GetChildren()) do
							if v then
								v:Destroy()
							end
						end
						
						local object2 = pet:Clone()
						object2.Parent = DisplayItem.ViewportFrame
						
						local camera = Instance.new("Camera")
						camera.CFrame = CFrame.new(object2.Part.Position + (object2.Part.CFrame.lookVector*5),object2.Part.Position)
						camera.Parent = DisplayItem.ViewportFrame
						
						DisplayItem.ViewportFrame.CurrentCamera = camera
						
						if plr.Character:FindFirstChild(pet.Name) then
							equipButton.Text = "Equipped"
						else
							equipButton.Text = "Equip"
						end
						
						local debounce = false
						equipButton.MouseButton1Click:Connect(function()
							if debounce == false then
								debounce = true
								local result = game.ReplicatedStorage.Remotes.EquipTool:InvokeServer(pet.Name)
								
								if result == true then
									equipButton.Text = "Equipped"
								end
							end
						end)
						module.openIFrame()
					end
				end
			end)
		end
	end
end

status:GetPropertyChangedSignal("Value"):Connect(function()
	if status.Value == true and TOnglet:FindFirstChild("Open").Value == false and HOnglet:FindFirstChild("Open").Value == false and POnglet:FindFirstChild("Open").Value == false then
		---- Run fonction ----
		toolDisplay()
		TOnglet:FindFirstChild("Open").Value = true
	else
		TOnglet:FindFirstChild("Open").Value = false
		HOnglet:FindFirstChild("Open").Value = false
		POnglet:FindFirstChild("Open").Value = false
	end
end)

TOnglet.MouseButton1Click:Connect(function()
	local open = TOnglet:FindFirstChild("Open")
	if open and open == false then
		---- Run open function ----
		toolDisplay()
		open.Value = true
		HOnglet:FindFirstChild("Open").Value = false
		POnglet:FindFirstChild("Open").Value = false
	end
end)

HOnglet.MouseButton1Click:Connect(function()
	local open = HOnglet:FindFirstChild("Open")
	print("Triggered")
	if open and open == false then
		---- Run open function ----
		print("Run")
		hatDisplay()
		open.Value = true
		TOnglet:FindFirstChild("Open").Value = false
		POnglet:FindFirstChild("Open").Value = false
	end
end)

POnglet.MouseButton1Click:Connect(function()
	local open = POnglet:FindFirstChild("Open")
	if open and open == false then
		---- Run open function ----
		petDisplay()
		open.Value = true
		TOnglet:FindFirstChild("Open").Value = false
		HOnglet:FindFirstChild("Open").Value = false
	end
end)

This goes in #help-and-feedback:scripting-support, code review is for polishing working code. Please read category guidelines in the future before posting.

Perhaps you could somehow narrow down to where the issue is? Try adding prints to see where the code stops working.

Ok thx you,
I’ll do that.
Have a nice day