when saving the playerdata when they leave the game, i find what items to remove by looping through their inventory from the datastore (which is the data from when they joined and from items that were added mid game) to the items they have in their inventory now, and check if there is an item that is in the datastore but not in the inventory so i can remove it from the datastore:
for i,v in pairs(playerData[plr.UserId]["Inventory"]["Swords"]) do
if plrinventory.Swords:FindFirstChild(v) == nil then
table.remove(playerData[plr.UserId]["Inventory"]["Swords"], table.find(playerData[plr.UserId]["Inventory"]["Swords"], v))
end
end
however upon testing i was having issues where if i removed items from a players inventory, lets say 20, it would only remove some of them. i was able to deduce that its because when looping through
playerData[plr.UserId][“Inventory”][“Swords”], it does not loop through all contents of the table. i know that all contents of the table that should be there are there because printing out that table would print out the lets say 20 items, but when doing print(i) in the for loop, it will only go to 11 or 12 or 13 or something like that