Trying to check if selected item is the same as another selected item

This is pretty simple but right now I’m trying to create a pet system that allows you to create a shiny pet so I have the selection done but now I’m trying to make it where it checks the already selected pets and sees if they all have the same ID if so select if not don’t select.

So far I’ve made it create a table that puts in the pets information UUID then info such as ID then if there’s more than one item in this table go through the table and check if the previous items have the same name but I cant figure out how to do that here’s my current attempt -


local function duplicatesExist(SelectedPet, pet)

	for i = 1, #SelectedPet do
		local v = SelectedPet[i]
		
		if v.ID == pet then return true
			else
		end
	end
end
				if table.getn(module.SelectedPet) == 5 then
				else
					if table.getn(module.SelectedPet) > 0 then
						if duplicatesExist(module.SelectedPet, clone.Name) == true then
							print("duplicate found")
							table.insert(module.SelectedPet, pet)
							clone.Delete.Visible = true
						else	
							print("no duplicates")
						end
					else
						table.insert(module.SelectedPet, pet)
						clone.Delete.Visible = true
					end
					
				end				

This is just an attempt on how I’m trying to get it to work but I can’t get it to work.
Thank you very much in advanced!

If you want to know if two pets have the same ID, you can do it like this:
local function duplicatesExist(SelectedPet, pet)
for i = 1, #SelectedPet do
if SelectedPet[i].ID == pet.ID then
return true
end
end
return false
end

If you want to know if all pets in the SelectedPet table have the same ID, you can do it like this:
local function sameId(SelectedPet, pet)
for i = 1, #SelectedPet do
if SelectedPet[i].ID ~= pet.ID then
return false
end
end
return true
end

1 Like

are you able to explain why the bottom one works?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.