My custom inventory script clone too much gui

Hi, i working on my custom inventory(backpack) system but when i added tools in my inventory, it’s clone too a lot and it clone all children as gui when one children in backpack added
Like in this video

Here my script :

local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:FindFirstChild("Humanoid")
local inputkeys = 
	{
	[1] = "One";
	[2] = "Two";
	[3] = "Three";
	[4] = "Four";
	[5] = "Five";
	[6] = "Six";
	[7] = "Seven";
	[8] = "Eight";
	[9] = "Nine";
	
}
local inputOrder = {
	inputkeys[1],inputkeys[2],inputkeys[3],inputkeys[4],inputkeys[5],
	inputkeys[6],inputkeys[7],inputkeys[8],inputkeys[9]
}
local tools = plr.Backpack:GetChildren()
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
function updateSlot()
	for i, child in pairs(script.Parent:GetChildren()) do
		for i, tool in pairs(tools) do
			if i > 9 then return end
			local clonegui = script.Parent.slot:Clone()
			clonegui.Name = tool.Name
			clonegui.Slot.Text = i
			clonegui.Name = tool.Name
			clonegui.Parent = script.Parent
			clonegui.Visible = true
			
			if tool.TextureId ~= "" then
				clonegui.ImageLabel.Image = tool.TextureId
				clonegui.ImageLabel.Visible = false
			elseif tool.TextureId == "" then
				clonegui.ImageLabel.Visible = false
				clonegui.Itemname.Text = tool.Name
			
			end
			clonegui.MouseEnter:Connect(function()
				if tool.ToolTip ~= "" then
					clonegui.Toolstipframe.ToolTips.Text = tool.ToolTip
					clonegui.Toolstipframe.Visible = true
				end
				clonegui.MouseLeave:Connect(function()
					clonegui.Toolstipframe.Visible = false
				end)
		end)
		end
		end
end
function handleequip(tool)
	if tool then	
		if tool.Parent ~= char then
			hum:EquipTool(tool)
		else
			hum:UnequipTools()
		end
	end
end

plr.Backpack.ChildAdded:Connect(function(child)
	if not table.find(tools, child) then
		table.insert(tools, child)
		updateSlot()
	end
end)
plr.Backpack.ChildRemoved:Connect(function(child)
	if child.Parent ~= char then
		table.remove(tools, tools[child])
		updateSlot()
	end
end)
plr.Backpack.ChildAdded:Connect(function(child)
	if child:IsA("Tool") and not table.find(tools, child) then
		table.insert(tools, child)
		updateSlot()
	end
end)
plr.Backpack.ChildRemoved:Connect(function(child)
	if child.Parent ~= plr.Backpack then
		table.remove(tools, tools[child])
		updateSlot()
	end
end)

Thank

1 Like

I see 2 issues with this and I will go into depth on both.

First of all, there is a better way to update the tools table. Instead of using insert and remove, you can just re-reference to backpack:GetChildren()

Second, more importantly, the updateSlot() function does not remove the old buttons. It needs to first clear the buttons, then add new ones.

With that in mind, here is your code corrected (only to fix the issue you mentioned):

local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:FindFirstChild("Humanoid")
local inputkeys = 
	{
		[1] = "One";
		[2] = "Two";
		[3] = "Three";
		[4] = "Four";
		[5] = "Five";
		[6] = "Six";
		[7] = "Seven";
		[8] = "Eight";
		[9] = "Nine";

	}
local inputOrder = {
	inputkeys[1],inputkeys[2],inputkeys[3],inputkeys[4],inputkeys[5],
	inputkeys[6],inputkeys[7],inputkeys[8],inputkeys[9]
}
local tools = plr.Backpack:GetChildren()
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
function updateSlot()
	tools = plr.Backpack:GetChildren()
	for i, child in pairs(script.Parent:GetChildren()) do
		child:Destroy()
		for i, tool in pairs(tools) do
			if i > 9 then return end
			local clonegui = script.Parent.slot:Clone()
			clonegui.Name = tool.Name
			clonegui.Slot.Text = i
			clonegui.Name = tool.Name
			clonegui.Parent = script.Parent
			clonegui.Visible = true

			if tool.TextureId ~= "" then
				clonegui.ImageLabel.Image = tool.TextureId
				clonegui.ImageLabel.Visible = false
			elseif tool.TextureId == "" then
				clonegui.ImageLabel.Visible = false
				clonegui.Itemname.Text = tool.Name

			end
			clonegui.MouseEnter:Connect(function()
				if tool.ToolTip ~= "" then
					clonegui.Toolstipframe.ToolTips.Text = tool.ToolTip
					clonegui.Toolstipframe.Visible = true
				end
				clonegui.MouseLeave:Connect(function()
					clonegui.Toolstipframe.Visible = false
				end)
			end)
		end
	end
end
function handleequip(tool)
	if tool then	
		if tool.Parent ~= char then
			hum:EquipTool(tool)
		else
			hum:UnequipTools()
		end
	end
end

plr.Backpack.ChildAdded:Connect(updateSlot)
plr.Backpack.ChildRemoved:Connect(updateSlot)

Let me know how this works

1 Like

Hi, your script is worked, But there is a problem, When the child added in inventory first times it’s cloned gui, but second time it not cloned gui

I’m not fully understanding what you’re trying to say. Could you try to clarify a bit more? Sorry.

1 Like

Hi, sorry for my bad grammar, When the child added in backpack it clone only once times
Here video

Any errors in the console? Unless there must be something else wrong that I’m missing.

1 Like

Apologies, I missed something.

Try this:

local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:FindFirstChild("Humanoid")
local inputkeys = 
	{
		[1] = "One";
		[2] = "Two";
		[3] = "Three";
		[4] = "Four";
		[5] = "Five";
		[6] = "Six";
		[7] = "Seven";
		[8] = "Eight";
		[9] = "Nine";

	}
local inputOrder = {
	inputkeys[1],inputkeys[2],inputkeys[3],inputkeys[4],inputkeys[5],
	inputkeys[6],inputkeys[7],inputkeys[8],inputkeys[9]
}
local tools = plr.Backpack:GetChildren()
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
function updateSlot()
	tools = plr.Backpack:GetChildren()
	for i, child in pairs(script.Parent:GetChildren()) do
		child:Destroy()
	end
	for i, tool in pairs(tools) do
		if i > 9 then return end
		local clonegui = script.Parent.slot:Clone()
		clonegui.Name = tool.Name
		clonegui.Slot.Text = i
		clonegui.Name = tool.Name
		clonegui.Parent = script.Parent
		clonegui.Visible = true

		if tool.TextureId ~= "" then
			clonegui.ImageLabel.Image = tool.TextureId
			clonegui.ImageLabel.Visible = false
		elseif tool.TextureId == "" then
			clonegui.ImageLabel.Visible = false
			clonegui.Itemname.Text = tool.Name

		end
		clonegui.MouseEnter:Connect(function()
			if tool.ToolTip ~= "" then
				clonegui.Toolstipframe.ToolTips.Text = tool.ToolTip
				clonegui.Toolstipframe.Visible = true
			end
			clonegui.MouseLeave:Connect(function()
				clonegui.Toolstipframe.Visible = false
			end)
		end)
	end
end
function handleequip(tool)
	if tool then	
		if tool.Parent ~= char then
			hum:EquipTool(tool)
		else
			hum:UnequipTools()
		end
	end
end

plr.Backpack.ChildAdded:Connect(updateSlot)
plr.Backpack.ChildRemoved:Connect(updateSlot)

Let me know if you’d like me to explain what I changed.

1 Like

Oh, i forgot open console it said “Players.GuHamYaiXD.PlayerGui.MainGui.ItemFrame.LocalScript:31: attempt to index nil with ‘slot’”

Also this is new script that i write

local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:FindFirstChild("Humanoid")
local inputkeys = 
	{
		[1] = "One";
		[2] = "Two";
		[3] = "Three";
		[4] = "Four";
		[5] = "Five";
		[6] = "Six";
		[7] = "Seven";
		[8] = "Eight";
		[9] = "Nine";

	}
local inputOrder = {
	inputkeys[1],inputkeys[2],inputkeys[3],inputkeys[4],inputkeys[5],
	inputkeys[6],inputkeys[7],inputkeys[8],inputkeys[9]
}
local tools = plr.Backpack:GetChildren()
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
function updateSlot()
	tools = plr.Backpack:GetChildren()
	for i, child in pairs(script.Parent:GetChildren()) do
		if not child:IsA("UIListLayout") then
			child:Destroy()
			end
	end
	for i, tool in pairs(tools) do
		if i > 9 then return end
		local clonegui = script.Parent.slot:Clone()
		clonegui.Name = tool.Name
		clonegui.Slot.Text = i
		clonegui.Name = tool.Name
		clonegui.Parent = script.Parent
		clonegui.Visible = true

		if tool.TextureId ~= "" then
			clonegui.ImageLabel.Image = tool.TextureId
			clonegui.ImageLabel.Visible = false
		elseif tool.TextureId == "" then
			clonegui.ImageLabel.Visible = false
			clonegui.Itemname.Text = tool.Name

		end
		clonegui.MouseEnter:Connect(function()
			if tool.ToolTip ~= "" then
				clonegui.Toolstipframe.ToolTips.Text = tool.ToolTip
				clonegui.Toolstipframe.Visible = true
			end
			clonegui.MouseLeave:Connect(function()
				clonegui.Toolstipframe.Visible = false
			end)
		end)
	end
end
function handleequip(tool)
	if tool then	
		if tool.Parent ~= char then
			hum:EquipTool(tool)
		else
			hum:UnequipTools()
		end
	end
end

plr.Backpack.ChildAdded:Connect(updateSlot)
plr.Backpack.ChildRemoved:Connect(updateSlot)

Ohhhhhh that is my mistake, I will fix that real quick, apologies.

1 Like

@Hamorize This should be it!

local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:FindFirstChild("Humanoid")
local inputkeys = 
	{
		[1] = "One";
		[2] = "Two";
		[3] = "Three";
		[4] = "Four";
		[5] = "Five";
		[6] = "Six";
		[7] = "Seven";
		[8] = "Eight";
		[9] = "Nine";

	}
local inputOrder = {
	inputkeys[1],inputkeys[2],inputkeys[3],inputkeys[4],inputkeys[5],
	inputkeys[6],inputkeys[7],inputkeys[8],inputkeys[9]
}
local tools = plr.Backpack:GetChildren()
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
function updateSlot()
	tools = plr.Backpack:GetChildren()
	for i, child in pairs(script.Parent:GetChildren()) do
		if child:FindFirstChild("Slot") and child.Name~="slot" then
			child:Destroy()
		end
	end
	for i, tool in pairs(tools) do
		if i > 9 then return end
		local clonegui = script.Parent.slot:Clone()
		clonegui.Name = tool.Name
		clonegui.Slot.Text = i
		clonegui.Name = tool.Name
		clonegui.Parent = script.Parent
		clonegui.Visible = true

		if tool.TextureId ~= "" then
			clonegui.ImageLabel.Image = tool.TextureId
			clonegui.ImageLabel.Visible = false
		elseif tool.TextureId == "" then
			clonegui.ImageLabel.Visible = false
			clonegui.Itemname.Text = tool.Name

		end
		clonegui.MouseEnter:Connect(function()
			if tool.ToolTip ~= "" then
				clonegui.Toolstipframe.ToolTips.Text = tool.ToolTip
				clonegui.Toolstipframe.Visible = true
			end
			clonegui.MouseLeave:Connect(function()
				clonegui.Toolstipframe.Visible = false
			end)
		end)
	end
end
function handleequip(tool)
	if tool then	
		if tool.Parent ~= char then
			hum:EquipTool(tool)
		else
			hum:UnequipTools()
		end
	end
end

plr.Backpack.ChildAdded:Connect(updateSlot)
plr.Backpack.ChildRemoved:Connect(updateSlot)
1 Like

Thank!!, It worked่!่่่่่่่่่่่่่่่่่่่่่่่่่่่่่่

Glad I was able to help! Have a good day/night.

1 Like