How would I detect two of the same objects in the same parent?

Hey, I’m making a game and I’m getting this bug that duplicates my items because it doesn’t detect if there are 2 of this item from a table.

I’ve tried many ways to figure out how to detect 2 objects in the backpack/character. This is my current script which is basic, but doesn’t get the job done.

if plr.Backpack:FindFirstChild(valks[i][1]) or char:FindFirstChild(valks[i][1]) then else

This detects if there is already the same item inside it, but it doesn’t prevent from 2 items going in there, how would I make it figure out if 2 of the same objects are in the backpack/character?

2 Likes

How to Detect Duplicates


As you can see, DataModels do not have similarities when duped. They are separate and have different directory. In order to match duplicates, you could check its name with the other.

if plr.Backpack:FindFirstChild(valks[i][1].Name) or char:FindFirstChild(valks[i][1].Name) then else

3 Likes

It’s not a DataModel.

valk[i][1]

is the name of the tool

What is ‘valks’?
I presume you’re iterating through a table here, can we get an example of the table structure?

In the meantime, you could shorten this to an inline statement:

function Search(par,nm,list)
   list = list or {}
   local obj = par:FindFirstChild(nm)
   table.insert(list, obj)
   return obj and Search(par,nm,list) or list
end
for i,v in pairs(valks) do
    local name = v[1]
    local bpList = Search( plr.Backpack, name )
    local chList = Search( char, name )

   if #bpList + #chList > 2  then
      --duplicates
   end
end

To add up on @WingItMan’s request, how is the table modified? In other words, how do you add up the values to the table?

This is the table structure:

local valks = {
    {"RustyValk", "0"};
    {"CandyValk", "100"};
    {"IceValk", "500"};
    {"StripedValk", "1500"};
    {"OctaneValk","3000"};
    {"GhostValk","5000"};
    {"RainbowValk","10000"};
    {"Valk","15000"};
    {"GoldenValk","20000"};
    {"SparkleValk","30000"};
    {"BlazeValk","40000"};
    {"CrystalValk","75000"};
    {"RedValk","100000"};
    {"BlackValk","175000"},
    
}

You could run the for loop around the backpack and the character itself.

-- Example
for _, v in next, character:GetChildren() do
    if v.Name == valks[i][1] then
        -- bool for each item to check, if this boolean was already true, just destroy it
        -- optionally, I think you could create a table for players to hold booleans of each object
    end
end

for _, v in next, player.Backpack:GetChildren() do
    if v.Name == valks[i][1] then
        -- bool for each item to check, if this boolean was already true, just destroy it, again
    end
end

Okay so first of all

if plr.Backpack:FindFirstChild(valks[i][1]) or char:FindFirstChild(valks[i][1]) then else

Is not the proper way to check if an item is not in there. You shouldn’t say then else if you want to do something when the opposite is true. Instead you should do something like

 if plr.Backpack:FindFirstChild(valks[i][1]) == nil or char:FindFirstChild(valks[i][1]) == nil then

But that code won’t even work in the first place. You need to keep track of how many items the player has in the backpack.

To accomplish that, I would have an int representing the amount of items found and increase it for each one that matches. If int is > 2, they have enough:

local int = 0

for i = 1, #valks do
    if player.Backpack:FindFirstChild(valks[i][1]) or player.Character:FindFirstChild(valks[i][1]) then
       int = int + 1
    end
end

if int <2 then
    --Do something.
end
2 Likes

A datamodel is game, so I’m not sure what it has to do with anything here.

1 Like

Okay, so try making a table to hold object instances, and then iterate through the table to see if the object inside the table is the original object. Hope this helps!