Can someone help me?

Hello, Can anyone tell me how I can delete all duplicate “Invalues”

I’m making a system using the for but I wanted it to create only once

    local Players = game:GetService("Players")

    local player = Players.LocalPlayer
    local char   = player.Character or player.CharacterAdded:Wait()
    local backpack = player.Backpack

    local function ScanItems()
    local items = {}

   --Scanning Backpack
for i, tool in pairs(backpack:GetChildren()) do
	if tool.ClassName == "Tool" then
		table.insert(items, tool)
	end
end

--Scanning Character
for i, tool in pairs(char:GetChildren()) do
	if tool.ClassName == "Tool" then
		table.insert(items, tool)
	end
end

--Creating New List
local invItems = {}

for i, tool in pairs(items) do
	if invItems[tool.Name] then
		table.insert(invItems[tool.Name].Items, tool)
	else
		invItems[tool.Name] = 
			{
			Items = {tool};
			}
	end
end
return invItems
 end

 
local function getNames()
local InvItems = ScanItems()

for i, item in pairs(InvItems) do
	local newValue = Instance.new("IntValue")
	newValue.Name = item.Items[1].Name
	newValue.Value = 0
	newValue.Parent = inventoryFolder
end
end

while true do
wait(5)
getnames()
end

What is the purpose of creating values when it seems you already have the data available? If you provide more information, I could provide you a better solution.

Create a table storing the data found and loop through the existing objects looking for if they were marked, if so then delete accordingly

local Values_Existing = {} 

for i,v in pairs(inventoryFolder) do 
	if Values_Existing[v] then
		v:Destroy()
	else
		Values_Existing[v] = true 
	end	
end

I’m doing an inventory system, and I’m using leaderstats to know the amount of the same item, but with the for it creates the same “Intvalue” over and over again

Its printing
Players.WolfSondar.PlayerGui.Inventory.InventoryScript:279: invalid argument #1 to ‘pairs’ (table expected, got Instance) - Cliente - InventoryScript:279

You could do something like this:

local function GetAmountOfItems(Items)
    local DataTable = {} -- Data table that is returned by the function.
    for _, Item in ipairs(Items) do -- Looping through each item.
        local Name = Item.Items[1].Name -- Name of the item.
        if DataTable[Name] then -- Checking if the item has already been added to the table.
            DataTable[Name] = DataTable[Name] + 1 -- If it already has, we add one to the value.
        else
            DataTable[Name] = 1 -- If it hasn't been added to the table, we set the value to one.
        end
    end
    return DataTable
end

This allows you get a table of the number of each item by name. This allows for easy fetching, without having to worry about creating & deleting instances.

local ItemCounts = GetAmountOfItems(InvItems)
local ItemCount = ItemCounts["Sword"] or 0

The above is an example of how you would get the count for the item “Sword”.

2 Likes

Thank You :slight_smile:

May I know how you learned this ?

1 Like

I just used my previous knowledge of tables, etc. When it comes to problems in programming you need to come up with solutions using the information you already know. However, when we don’t know something it’s great to post questions on forums such as this one as you’ll be able to learn from your mistakes and hopefully get a solution. :slight_smile:

Now, when you need to organize specific data, such as item amounts, you’ll have an idea where to start. And of course, there is plenty of great articles to read: Learn Roblox

Glad I could help you!

1 Like

Adorable jacob really adorable. Thanks for listening :heart:

Call the GetChildren method next time for iterating through children of instances.