How could I remove only 1 object when doing a loop

So, I have a table and I have ui’s. What the system does is if the ui’s text is found more than 1 time it adds the ui to the table. Now what I want it to do is only remove one ui that was found more than 1 time. I did object (which is q in this script) :Destroy() and it destroyed all of the ui’s. How could I make it only destroy the one found more than once?

task.spawn(function()
	while task.wait(1) do
		for i,q in pairs(VehicleScrollingFrame:GetChildren()) do
			if q:IsA("ImageLabel") then
					table.sort(CurrentCars)
					print(CurrentCars)
					local strings = 0
					for index,v in ipairs(CurrentCars) do
						if CurrentCars[index + 1] == v then
							q:Destroy()
						end
					end

			end

		end
	end
end)

1 Like

put a break statement after destroying

q:Destroy()
break
1 Like

Still removes all uis. No errors in the output

1 Like

the put a break in the initial if statement at the end after for loop has been done


			if q:IsA("ImageLabel") then
					table.sort(CurrentCars)
					print(CurrentCars)
					local strings = 0
					for index,v in ipairs(CurrentCars) do
						if CurrentCars[index + 1] == v then
							q:Destroy()
						end
					end
break****************
			end
1 Like

Now what it’s doing is deleting both uis the one the original and then the duplicated obj.

1 Like

Here is a video of it

1 Like

			if q:IsA("ImageLabel") then
					table.sort(CurrentCars)
					print(CurrentCars)
					local strings = 0
					for index,v in ipairs(CurrentCars) do
						if CurrentCars[index + 1] == v then
							q:Destroy()
							break****************
						end
					end
			end

Don’t you mean this?

1 Like

Im gonna be honest, I’m a little confused by your code,

when added a break after q:destroy() it still destroys everything, this shouldve been the place to put a break statement so you could move on to the next q object.

perhaps this is because it is under a while loop?

1 Like

I also did a while wait(1) do so thats why it keeps running

1 Like

Do you really need the while loop? once this code executes, it should remove any duplicates.

You could also just insert the object you want to keep into a table and then if table.find() the object within said table then continue for loop, else destroy

1 Like

Every one second it will destroy a car, that is why in the video you see that every 1 second a car disappears, also update the destroy to be like this (because you want it to break as soon as it destroys, because with the code before there was a risk of destroying more than one):


			if q:IsA("ImageLabel") then
					table.sort(CurrentCars)
					print(CurrentCars)
					local strings = 0
					for index,v in ipairs(CurrentCars) do
						if CurrentCars[index + 1] == v then
							q:Destroy()
							break****************
						end
					end
					break****************
			end
1 Like

I added a loop because it only works 1 time wiithout a loop

1 Like

I see what you mean now

task.spawn(function()
	while task.wait(1) do
		for i,q in pairs(VehicleScrollingFrame:GetChildren()) do
			if q:IsA("ImageLabel") then
					table.sort(CurrentCars)
					print(CurrentCars)
					local strings = 0
					local found = 0
					for index,v in ipairs(CurrentCars) do
						if CurrentCars[index + 1] == v  then
							if found == 0 then found += 1
							else q:Destroy() end
						end
					end
			end

		end
	end
end)

try that

1 Like

Didnt work :confused: Any other ideas??

So you want to remove any duplicate cars right?

task.spawn(function()
	while task.wait(1) do
		for i,q in pairs(VehicleScrollingFrame:GetChildren()) do
			if q:IsA("ImageLabel") then
					print(CurrentCars)
					local found = 0
					for _,v in ipairs(CurrentCars) do
						for _,v2 in ipairs(CurrentCars) do
							if v2 == v  then
								if found == 0 then found += 1
								else q:Destroy() end
							end
						end
					end
			end

		end
	end
end)

Try that, also why are you looping in vehicle scrolling frame and then looping in the table, you don’t modify the table then that means it will keep deleting.

What is CurrentCars?

1 Like

Current cars is just a table that i put all the strings into