"Equip Best" button not working!

Hello everyone!

I am making an “equip best” pet system in my simulator game. Unfortunately, it is not working!

I am looping through all of the pets the player has and putting that into a table. I then filter the first x (the number of pets a player can equip at once) terms in the table and put it into a new table. I then run the onEquip() function on each of the pets. The problem is that if a player has the same pet in their inventory and they both are the best pet in the game, either only one of them will get equipped, or none of them will get equipped!

I problem right now is that when I press the “equip best” button, it starts equipping random pets.

Function:

local function equipBest()
	local pets = {}
	local newPets = {}
	
	local maxPetsEquipped = player.Values.MaxPetsEquipped.Value
	
	for i, pet in pairs(container:GetChildren()) do
		if not pet:IsA("GuiButton") or pet.Name == "Template" then continue end
		
		local petName = pet.Name
		
		table.insert(pets, petName)
	end
	
	for i = 1, maxPetsEquipped do
		table.insert(newPets, pets[1])
		table.remove(pets, 1)
	end
	
	print(pets)
	print(newPets)
	
	for i, v in newPets do
		onEquip(v)
	end
end

Please help!
Thanks

-GreenTreeGaming

1 Like

i think the gui buttons are sorted with layout order

local function equipBest()
	local pets = {}
	local newPets = {}

	local maxPetsEquipped = player.Values.MaxPetsEquipped.Value

	for i, pet in pairs(container:GetChildren()) do
		if not pet:IsA("GuiButton") or pet.Name == "Template" then continue end
		table.insert(pets, pet)
	end

	table.sort(pets, function(PetButtonA, PetButtonB)
		return PetButtonA.LayoutOrder > PetButtonB.LayoutOrder
	end)

	for i = 1, maxPetsEquipped do
		if #pets <= 0 then
			break -- no more pets
		end
		
		table.insert(newPets, pets[1])
		table.remove(pets, 1)
	end

	print(pets)
	print(newPets)

	for i, v in newPets do
		onEquip(v)
	end
end

This is in a local script, or at least I assume so because we’re referencing player GUI. This logic needs to be on sever.

@BirdieI90 I will try that out and see if it works
@samjay22 I will try your idea out too!

@BirdieI90 Still doesn’t work :\

Here is a quick video to show you what is going on:

Thanks

-GreenTreeGaming