Pets are getting duplicated in table

Hello developers!

I am creating pet mass delete but when player selects one pet and another one then the first pet he selected is being duplicated.

This is how it looks (look at the Output):

script:

local petsToDelete = {}

local function onTemplatePress(temp)
	selectedTemplate = temp
	if isPlayerMassDeleting == true then
		if selectedTemplate:FindFirstChild("UIStroke") then
			selectedTemplate.UIStroke:Destroy()
			selectedTemplate.Mark:Destroy()
			
			local toRemove = table.find(petsToDelete, selectedTemplate)
			table.remove(petsToDelete, toRemove)
			
			NotificationText.Text = #petsToDelete .. " pets selected"
		else
			local stroke = Instance.new("UIStroke")
			stroke.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
			stroke.Color = Color3.fromRGB(255, 57, 57)
			stroke.Thickness = 3
			stroke.Parent = selectedTemplate
			
			local mark = Instance.new("TextLabel")
			mark.Size = UDim2.new(1,0,1,0)
			mark.BackgroundTransparency = 1 
			mark.TextColor3 = Color3.fromRGB(255, 57, 57)
			mark.Text = "X"
			mark.Font = Enum.Font.FredokaOne
			mark.TextScaled = true
			mark.Parent = selectedTemplate
			mark.Name = "Mark"
			
			for i, v in pairs(scroller:GetChildren()) do
				if v:FindFirstChild("Mark") and v:FindFirstChild("UIStroke") then
					table.insert(petsToDelete, v.Name)
				end
			end
			
			print(petsToDelete)
			NotificationText.Text = #petsToDelete .. " pets selected"
		end
	else
		PetInfo.Parent = temp
	end
	if selectedTemplate:FindFirstChild("Equipped").Value == true then
		
		Equip.Text = "Unequip"
	else
		
		Equip.Text = "Equip"
	end
	
	PetName.Text = temp.Name
	EnergyMulti.Text = "⚡x"..RepStor.Pets:FindFirstChild(temp.Name, true).Multiplier1.Value
end
2 Likes

So from what I understand of the script, you’re using this “Mark” object to show which ones should be deleted, and after the first one is marked the script will go through and find everything with marked and add it to the table, however when you press the second item and mark it the script will still acknowledge the first marked item and add it back doing its job, Idk if it would break the script or not but try removing the mark after the table adds whatever pet assuming you don’t need the mark to exist, or just change the name to “Marked” or something different.

2 Likes

If pet name will be changed to “PetName-Selected” or “Selected” then it will be the same problem.

1 Like

Not the pet name itself I’m talking about the mark the text label. because i’m assuming if i’m reading it right thats how you determine which pets are getting deleted so you loop through and see a mark but if you don’t change the name or give it some kind of marker the script will keep seeing it and adding it to the table.

here’s a basic example its not to be used its just for illustrating my point

local stuff = {pets}
local marker = mark

uiClicked:Connect(function(UI)
     local marker2 = marker:Clone()
     marker2.Parent = UI
end)

for i,v in pairs(table) do
     if v:FindFirstChild("mark") then --every time this loops back around no matter how many items you've marked it will still see the original marked items and add it to the list
          do stuff
     end
end

--however assuming marked isn't required for any other function other than that
for i,v in pairs(table) do
     if v:FindFirstChild("mark") then
          do stuff
          v.mark:Destroy() --you destroy the mark or whatever you're using to mark them and then when the script loops back around it won't see the mark and only add the newly marked one
     end
end
1 Like