Table.remove not working

when I tell the table to remove an element it doesn’t, there’s not errors also.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SelectedUnit = {}

function isInTable(toFind)
	local found = false
	for i,v in pairs(SelectedUnit) do
		if v==toFind then
			found = true
			print(i)
			table.remove(SelectedUnit,i) -- this is what is not working
			break;
		end
	end
	return found
end

ReplicatedStorage.ClientSelectedObject.OnClientEvent:Connect(function(UnitName)
	
	if isInTable(UnitName)then
		--DeselectObject
		print("deselectObject")
		game.Workspace:WaitForChild(UnitName).Transparency = 0
		
	else
		--SelectedObject
		print("IDK")
		SelectedUnit[#SelectedUnit] = UnitName
		game.Workspace:WaitForChild(UnitName).Transparency = 0.3
	end
	
	print(SelectedUnit)
end)

Are you sure that it isn’t working? table.remove will move all the following indexes up after it removes the desired index, meaning SelectedUnit[i] may still be in use after table.remove, but by another value.

I don’t understand how it could still be in use?

" Variant table.remove ( array t, number pos = #t )

Removes from array t the element at position pos, returning the value of the removed element. When pos is an integer between 1 and #t, it shifts down the elements t[pos+1], t[pos+2], …, t[#t] and erases element t[#t]. The index pos can also be 0 when #t is 0 or #t+1; in those cases, the function erases the element t[pos]."

To fix it, instead of using table.remove, use SelectedUnit[i] = nil