Template duplicates

This script makes it so that every time a player opens their inventory it creates a template for each item in their inventory. But if you spam the inventory button there is only one item in their inventory but it keeps making more templates how could I check and stop this from happening?

local Player = game.Players.LocalPlayer
local Inventory = Player:WaitForChild("Inventory")
local motherboardFrame = Player.PlayerGui.WorkshopGui.MotherboardFrame.ScrollingFrame
local coolerFrame = Player.PlayerGui.WorkshopGui.CoolerFrame.ScrollingFrame
local cpuFrame = Player.PlayerGui.WorkshopGui.CpuFrame.ScrollingFrame
local gpuFrame = Player.PlayerGui.WorkshopGui.GpuFrame.ScrollingFrame
local powerSupplyFrame = Player.PlayerGui.WorkshopGui.PowerSupplyFrame.ScrollingFrame
local ramFrame = Player.PlayerGui.WorkshopGui.RamFrame.ScrollingFrame
local storageFrame = Player.PlayerGui.WorkshopGui.StorageFrame.ScrollingFrame
local template = Player.PlayerGui.WorkshopGui.Template
local InventoryButton = Player.PlayerGui:WaitForChild("WorkshopButtons").Inventory
local ItemParenting = {
	Motherboard = motherboardFrame,
	Cooler = coolerFrame,
	Cpu = cpuFrame,
	Gpu = gpuFrame,
	PowerSupply = powerSupplyFrame,
	Ram = ramFrame,
	Storage = storageFrame
}

InventoryButton.MouseButton1Click:Connect(function(player)
	for _, item in pairs(Inventory:GetChildren()) do
		local newTemplate = template:Clone()
		newTemplate.Name = item.Name
		newTemplate.ObjectName.Text = item.Name
		newTemplate.Visible = true
		
		local Item69
		local Item420
		
		for name, parent in pairs(ItemParenting) do
			if string.match(item.Name, name) then
				newTemplate.Parent = parent
				Item69 = name
				Item420 = parent
				break
			end
		end

		newTemplate.MouseButton1Click:Connect(function()
			local Check = game.ReplicatedStorage.Inventory:InvokeServer(Player, item.Name, Item69)
			if Check == true then
				Player.Inventory[item.Name]:Destroy()
				Player.PlayerGui.WorkshopGui[Item420.Parent.Name].ScrollingFrame[item.Name]:Destroy()
				Player.PlayerGui.WorkshopGui.Enabled = false
			end
		end)
	end
end)
2 Likes

Just clear all of the children of the GUI’s storage stuff (idk how to put it).

InventoryButton.MouseButton1Click:Connect(function(player)
	for i, parent in pairs(Itemparenting) do
		parent:ClearAllChildren()
	end
	for _, item in pairs(Inventory:GetChildren()) do
		local newTemplate = template:Clone()
		newTemplate.Name = item.Name
		newTemplate.ObjectName.Text = item.Name
		newTemplate.Visible = true
		
		local Item69
		local Item420
		
		for name, parent in pairs(ItemParenting) do
			if string.match(item.Name, name) then
				newTemplate.Parent = parent
				Item69 = name
				Item420 = parent
				break
			end
		end

		newTemplate.MouseButton1Click:Connect(function()
			local Check = game.ReplicatedStorage.Inventory:InvokeServer(Player, item.Name, Item69)
			if Check == true then
				Player.Inventory[item.Name]:Destroy()
				Player.PlayerGui.WorkshopGui[Item420.Parent.Name].ScrollingFrame[item.Name]:Destroy()
				Player.PlayerGui.WorkshopGui.Enabled = false
			end
		end)
	end
end)

but that will just clear the players inventory and I don’t really want that. How would I check for it to see if a template was already made for that item

1 Like

It won’t clear the player’s inventory as you are going through the whole inventory and making new templates for all of the items. That is why I cleared it before that loop.

it kinda works because if I have multiple of the same item it will only create 1 template but it does not multiply anymore

I’m not exactly sure as to why that would be. You never checked whether there was one already there after clearing the children, right?

in game I had 3 of the same part but only 1 template