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)
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)
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.
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)
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)