# Finding the top 5 values of a table

So I am making a pet system where I can press a equip best button and the script will find the best 5 pets I have and equip them. This is the code I have

``````script.Parent.MouseButton1Click:Connect(function()
local pets = Player.Pets:GetChildren()

table.sort(pets, function(pet1, pet2)
local average1 = (pet1.Multiplier1.Value + pet1.Multiplier2.Value) / 2
local average2 = (pet2.Multiplier1.Value + pet2.Multiplier2.Value) / 2
return average1 > average2
end)
``````

Right now all this does is take all the pets the player has and puts them in a table then sorts them highest value to least value. Now what I want to do is figure out how I can equip only the top 5. So I guess if someone can help me figure out how to like lets say print the first 5 values that would be very helpful.

1 Like
``````-- after sorting
if #pets == 0 then
return print'You have no pets'
end

for i = 1, math.min(#pets, 5) do -- clamp i (in case you have less than 5 pets)
local pet = pets[i] -- get pet by index, assuming 1 being the best pet, 2 being the 2nd best, etc.
print(pet)
end
``````

Ok then lets say that I wanted to do something specific to each one how do you think i would do that. Like lets say each pet was a part and I wanted to change the brickcolor of the first one to black and second to white etc…

1 Like

You could make a table of colors and then match the pet index to the color index