How do I handle data duplicates?

So I’m making a tower defense game and its a gacha system. I wanna make it so you can have multiple of the same tower so you can sell them but you can’t equip more than one of each tower. The data is split into SelectedTowers and OwnedTowers and I have this code to get the status:

local function getItemStatus(itemName)
	if table.find(playerData.SelectedTowers, itemName) then
		return "Equipped"
	elseif table.find(playerData.OwnedTowers, itemName) then
		return "Owned"

The issue is that all duplicates of one tower are gonna return “Selected” even if only one of them is and the rest are just Owned. How can I fix this?

1 Like

How are you marking the towers as Selected or Owned? Where is the code that does this?

1 Like

This is where you insert it into the table


And the actual tower thing looks like this:

["Cameraguy"] = {
				["Name"] = "Cameraguy",
				["ImageAsset"] = "",
			["Rarity"] = "Common",
			["Chance"] = 52.5

So I insert the name of it

Do they all have same name? Also if a tower is moved to Owned/Selected and that status changes how is that handled?

They do all have the same name and the status doesn’t really change it just moves to a different “parent”

So when you add the name of the tower to a table. That’s just a string. Might be easier to find selected vs owned by searching the parent object of the tower.


Then you know anything coming from the owned parent is an owned tower object.

I ended up doing this:

local alreadyFound = {}

	for i, v in pairs(inventoryFrame:GetChildren()) do
		if v:IsA("Frame") and v.Name ~= "Template" then

	for i, key in pairs(playerData.OwnedTowers) do
		local tower = getTowerByKey(key)		
		local status = getItemStatus(tower.Name,alreadyFound)
		if status == "Equipped" then
			newButton.ImageTemplate.Checkmark.Visible = true
			newButton.LayoutOrder = towerfromRs.Config.Price.Value

		elseif status == "Owned" then
			newButton.ImageTemplate.Checkmark.Visible = false
			newButton.LayoutOrder = (towerfromRs.Config.Price.Value) + 999999999

And then in the getStatus function:

local function getItemStatus(itemName,alreadyFound)
	print(alreadyFound,"Before insert")
	if table.find(playerData.SelectedTowers, itemName) and not table.find(alreadyFound,itemName) then
		print(alreadyFound,"After we insert")

		return "Equipped"
	elseif table.find(playerData.OwnedTowers, itemName) then
		return "Owned"

Is this efficient? I create a new table each time and use it to identify if something has already been returned as “Equipped”

Tbh its hard to say without knowing more about what’s all going on with the scripts. But in my head I’m thinking just adding an attribute to the object “Status”, “Owner” and then setting those values would be easiest and most efficient method to keep track of them. If the object doesn’t exist when its not “Selected” then just keeping a tally of how many owned would be even more efficient. Then is just a single variable with a number count of how many the have. You can adjust that number as they are used.