Sorting Table - Issue

Hello Devs,
I’ve been recently trying to make something like “Equip Best” for my game - basically select Multipliers of Pet, sort the Pet in table from highest to lowest depends on the Multiplier and then select the best ones.
Issue:
I often get two values for one key because the Multipliers of the two pets are same and it cause every other pet after that not able to equip.

Code:

for idx = 1,Player.Data.MaxEquip.Value do
	local key = PetTable[idx]
	for index,choose in pairs(PetTable) do
		local TotalEquipped = 0
		for i,v in pairs(Player.Pets:GetChildren()) do
				if v.Equipped.Value == true then	
					TotalEquipped = idx		
				end
			end
		if TotalEquipped < Player.Data.MaxEquip.Value and choose == key then
				--TotalEquipped = TotalEquipped + 1
				petsequip[#petsequip + 1] = choose
				Player.Pets:FindFirstChild(choose.Name).Equipped.Value = true
				loadEquipped(Player)
			end
		end
	end
	return petsequip
end

--

if Action == "Equip Best" then
		local AllPets = Player.Pets:GetChildren()
		table.sort(AllPets, function(a: Pet, b: Pet)
			return a.Multiplier1.Value + a.Multiplier2.Value > b.Multiplier1.Value + b.Multiplier2.Value
		end)
		local Result = EquipBest(Player,AllPets)
		return Result
	end

Any help is much appreciated :wink:!

1 Like

first of can you print the

a.Multiplier1.Value + a.Multiplier2.Value > b.Multiplier1.Value + b.Multiplier2.Value

Alright.
Prints:

  02:06:42.841  true  -  Server - PetHandler:309
  02:06:42.841   ▶ false (x6)  -  Server - PetHandler:309
  02:06:42.844  true  -  Server - PetHandler:309
  02:06:42.844   ▶ false (x6)  -  Server - PetHandler:309

that should all be true right? because if false then it will not equip the pet

Yes but as I mentioned before if the Multipliers are same then it will have two values inside one key so something like this:

[1] = Pet1,Pet3

And stop equip other pets after that.

is there any reason why there is a multiplier 2?

Yes because each pet has two Multipliers.

if the multipliers are the same then just return one of the multipliers

Okay I will explain it better.
Each pet has 2 Multipliers - each Multiplier is different.
But if two pets have same Multiplier it will give them into one key in the table and that is the issue.

1 Like

if they have the same multiplier then make a new table with the 2nd pet or just something like this

pet[#pet + 1] = 2ndPet

also try >=

Okay but how I am suppose to indicate when the key have two pets inside.
And second if you mean the if statement then its completely fine because its equipping the pet after that part and if there will be >= it will go above the player maximum equipped.

I was proposing an idea of 1 table for equal multipliers and 1 for different multipliers

if 2 pets have the same multiplier then it will go to the equal table and if they have different multipliers then they go to the different table

but that only works for 1 multiplier

1 Like

Thank you for an idea… but with these 2 Multipliers is there way to make it? Whats even the problem of the script when there are two pets with same multiplier inside one key?..

its because the sorting function thinks that if they are the same multiplier then it says go to the same key we just assigned, another idea is to make a table of best pets and then just clean it up after using it

2 Likes

Can you please provide me with example of the “cleaning”?

if the table is used then just make it nil or just create a new one when sorting it

1 Like

Sorry for late response but how do I achieve if the key in the table is already used?

you can just add a property if its used