Table Items aren't removing properly?

So, I’m making a round based survival with an objective, the first team’s objective is to collect all items and escape.

so i implemented the items, only i did it the ghetto way and now items don’t remove themselves properly from the table as seen here below.
image

as you see in this video, the number doesn’t drop below 3 initially and once i collect all of the nodes, it’s still not zero, this varies each play test, so i can somehow collect all 4 but not the one.

I don’t know what’s the cause of it, could you help out?

This is how each player receives nodes, server gives each player 5 of those:

function Handler:GetEachScientistAnItem()

    local nodes = {}

    local runningMap = workspace:FindFirstChild(MapList[MapIndex])

    if runningMap then
        local Nodes = runningMap.Nodes:GetChildren()
        local Scientists = T.Scientist:GetPlayers()

        for index, node in pairs(Nodes) do
            table.insert(nodes, Nodes[index])
        end
        
        for i = 1, #Scientists do
            local playerNodes = {}

            for j = 1, 5 do
                local randomIndex = math.random(#nodes)
                table.insert(playerNodes, nodes[randomIndex])
                table.remove(nodes, randomIndex)
                print(nodes)
            end
            
            
            Remotes.GiveEachAnItem:FireClient(Scientists[i], playerNodes)
        end
        print(nodes)
    end
end

and this is how client handles those nodes, keep in mind each set of nodes for each player is unique so only the player who received them can collect them:

function updateCount(number)
    Core.Parent.Collect.Text = number
end

function FindItems(Table: {any})
    
    local Saved = Table
    print("hey look what server gave me:")
    print(Saved)
    
    for index, thing: BasePart in pairs(Saved) do
        thing.Color = Color3.fromRGB(math.random(0, 255), math.random(0, 255), math.random(0, 255))
        thing.Touched:Connect(function()
            table.remove(Saved, index)
            updateCount(#Saved)
            print(Saved)
            thing:Destroy()
        end)
    end
end

Remotes.GiveEachAnItem.OnClientEvent:Connect(FindItems)
1 Like

You are using outdated indexes.
For example, lets say I have this table:
{“apple”, “banana”, “orange”, “pear”}
Apple has an index of 1, banana has an index of 2, orange has an index of 3, pear has an index of 4.
If I want to remove the banana, I would do table.remove(fruitTable, 2).
But now the table looks like this:
{“apple”, “orange” pear”}
Because I removed the banana, the other values shifted down an index. Now the orange has an index of 2 and the pear and index of 3.
What your issue is that you “precalculate”/store the old index values, which works for the first node, but not necessarily for the later nodes.
So try using table.find to find the correct index and then remove it:
table.remove(Saved, table.find(Saved, thing))

2 Likes

You’re a saviour mate, thank you!!! I should’ve double checked, man I feel dumb now.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.