Hello so I ran into this problem where when the script checks the player’s backpack, the tools detected are inserted more than once, for example I have a mop in my backpack, the script inserts the mop to a table multiple times, this is because the check backpack script is in a loop to make sure all tools are detected just incase a tool is added later on the game. How do I make it so the tool is only inserted once.
local function checkBackpack()
for i, v in pairs(backpack:GetChildren()) do
if v.ClassName == "Tool" then
table.insert(InventoryItems, v)
end
end
for i, v in pairs(Character:GetChildren()) do
if v.ClassName == "Tool" then
table.insert(InventoryItems, v)
end
end
end
I would create a function to check if it’s data is in the table, and if it finds the data then it won’t add it in. If it helps you can also do a task.wait(n) in it to.
Feel free to let me know if you have any problems.
I would create a function that is called checkIfInData() and then code it in to check every heart beat of so if the data is in the table to not re add it.
Simply just check if the tool is already in the table. If yes then just stop the thing from adding.
For this purpose, we use table.find(table, thingtofind) which returns a number position. It will be nil if the thing to find isn’t in the table but if it is in the table, the value won’t be nil meaning you can stop the code from running further if you use return if the script has a value from the table.find statement which is not nil.
local function checkBackpack()
for i, v in pairs(backpack:GetChildren()) do
if v.ClassName == "Tool" then
if table.find(InventoryItems, v) then return end -- stops if the tool is found in the table
table.insert(InventoryItems, v)
end
end
for i, v in pairs(Character:GetChildren()) do
if v.ClassName == "Tool" then
if table.find(InventoryItems, v) then return end -- stops if the tool is found in the table
table.insert(InventoryItems, v)
end
end
end
local function checkBackpack()
for i, v in pairs(backpack:GetChildren()) do
if v.ClassName == "Tool" then
if table.find(InventoryItems, v) then return end
table.insert(InventoryItems, v)
end
end
for i, v in pairs(Character:GetChildren()) do
if v.ClassName == "Tool" then
if table.find(InventoryItems, v) then return end
table.insert(InventoryItems, v)
end
end
end
local slots = (hotBar)
local hotbarItems = {}
local InventoryItems = {}
local backpack = Player.Backpack
local function checkBackpack()
for i, v in pairs(backpack:GetChildren()) do
if v.ClassName == "Tool" then
if table.find(InventoryItems, v) then return end
table.insert(InventoryItems, v)
end
end
for i, v in pairs(Character:GetChildren()) do
if v.ClassName == "Tool" then
if table.find(InventoryItems, v) then return end
table.insert(InventoryItems, v)
end
end
end
local itemChosen = nil
local human = Character:WaitForChild("Humanoid")
task.spawn(function()
while task.wait(0.1) do
checkBackpack()
local limit = #InventoryItems
if limit == 0 then continue end
for i, v in pairs(InventoryItems) do
local newSample = sample:Clone()
newSample.Parent = script.Parent.BackpackUI.ScrollingChoices
newSample.Name = v.Name; newSample.Visible = true
newSample.Image = v.TextureId
local backpackRemove = table.remove(InventoryItems, 1)
if not backpackRemove then continue end
itemChosen = newSample.MouseButton1Click:Connect(function()
script.Parent.Backpack:TweenPosition(UDim2.new(0.394, 0,0.824, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
if #hotbarItems == 0 then
wait(0.2)
hotBar.Visible = true
hotBar.Image = newSample.Image
hotBar.Item.Value = backpack[newSample.Name]
table.insert(hotbarItems, newSample)
newSample.Parent = script.HoldingOn
local tool = hotBar.Item.Value
human:EquipTool(backpack[newSample.Name])
elseif #hotbarItems == 1 then
human:UnequipTools()
for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
end
hotBar.Image = newSample.Image
hotBar.Item.Value = backpack[newSample.Name]
human:EquipTool(backpack[newSample.Name])
newSample.Parent = script.HoldingOn
end
end)
script.Parent.Hotbar.Exit.MouseButton1Click:Connect(function()
script.Parent.Backpack:TweenPosition(UDim2.new(0.467, 0,0.826, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
human:UnequipTools()
leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
table.remove(hotbarItems, 1)
hotBar.Image = "rbxassetid://10453314827"
hotBar.Item.Value = nil
hotBar.Visible = false
end
end)
end
end
end)
local slots = (hotBar)
local hotbarItems = {}
local InventoryItems = {}
local backpack = Player.Backpack
local function checkBackpack()
for i, v in pairs(backpack:GetChildren()) do
if v.ClassName == "Tool" then
if table.find(InventoryItems, v) then continue end
table.insert(InventoryItems, v)
end
end
for i, v in pairs(Character:GetChildren()) do
if v.ClassName == "Tool" then
if table.find(InventoryItems, v) then continue end
table.insert(InventoryItems, v)
end
end
end
local itemChosen = nil
local human = Character:WaitForChild("Humanoid")
task.spawn(function()
while task.wait(0.1) do
checkBackpack()
local limit = #InventoryItems
if limit == 0 then continue end
for i, v in pairs(InventoryItems) do
local newSample = sample:Clone()
newSample.Parent = script.Parent.BackpackUI.ScrollingChoices
newSample.Name = v.Name; newSample.Visible = true
newSample.Image = v.TextureId
local backpackRemove = table.remove(InventoryItems, 1)
if not backpackRemove then continue end
itemChosen = newSample.MouseButton1Click:Connect(function()
script.Parent.Backpack:TweenPosition(UDim2.new(0.394, 0,0.824, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
if #hotbarItems == 0 then
wait(0.2)
hotBar.Visible = true
hotBar.Image = newSample.Image
hotBar.Item.Value = backpack[newSample.Name]
table.insert(hotbarItems, newSample)
newSample.Parent = script.HoldingOn
local tool = hotBar.Item.Value
human:EquipTool(backpack[newSample.Name])
elseif #hotbarItems == 1 then
human:UnequipTools()
for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
end
hotBar.Image = newSample.Image
hotBar.Item.Value = backpack[newSample.Name]
human:EquipTool(backpack[newSample.Name])
newSample.Parent = script.HoldingOn
end
end)
script.Parent.Hotbar.Exit.MouseButton1Click:Connect(function()
script.Parent.Backpack:TweenPosition(UDim2.new(0.467, 0,0.826, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
human:UnequipTools()
leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
table.remove(hotbarItems, 1)
hotBar.Image = "rbxassetid://10453314827"
hotBar.Item.Value = nil
hotBar.Visible = false
end
end)
end
end
end)
I’ve printed #InventoryItems and I get one so I’m now thinking it’s something to do with, can you take a look:
task.spawn(function()
while task.wait(0.1) do
checkBackpack()
local limit = #InventoryItems
if limit == 0 then continue end
for i, v in pairs(InventoryItems) do
local newSample = sample:Clone()
newSample.Parent = script.Parent.BackpackUI.ScrollingChoices
newSample.Name = v.Name; newSample.Visible = true
newSample.Image = v.TextureId
local backpackRemove = table.remove(InventoryItems, 1)
if not backpackRemove then continue end
itemChosen = newSample.MouseButton1Click:Connect(function()
script.Parent.Backpack:TweenPosition(UDim2.new(0.394, 0,0.824, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
if #hotbarItems == 0 then
wait(0.2)
hotBar.Visible = true
hotBar.Image = newSample.Image
hotBar.Item.Value = backpack[newSample.Name]
table.insert(hotbarItems, newSample)
newSample.Parent = script.HoldingOn
local tool = hotBar.Item.Value
human:EquipTool(backpack[newSample.Name])
elseif #hotbarItems == 1 then
human:UnequipTools()
for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
end
hotBar.Image = newSample.Image
hotBar.Item.Value = backpack[newSample.Name]
human:EquipTool(backpack[newSample.Name])
newSample.Parent = script.HoldingOn
end
end)
script.Parent.Hotbar.Exit.MouseButton1Click:Connect(function()
script.Parent.Backpack:TweenPosition(UDim2.new(0.467, 0,0.826, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
human:UnequipTools()
leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
table.remove(hotbarItems, 1)
hotBar.Image = "rbxassetid://10453314827"
hotBar.Item.Value = nil
hotBar.Visible = false
end
end)
end
end
end)
Backpack.ChildAdded:connect(function(child)
end
Backpack.ChildRemoved:connect(function(child)
end
And inside check if its in the table, if it is then dont clone if it isn’t in the table than add it and clone, in the removing just remove its button and remove it from the table.