I’m currently making the equip best pet button in my game, but I ran into an issue. Each pet has an IntValue called “Rarity” inside them. The better the rarity, the higher the number, maxing out at number 5. After looping through all of the pets, I don’t know how I can get the values with the highest number. For example, if a player has one pet with rarity 5, two pets with rarity 4, and three pets with rarity 1, then I would need to get one rarity 5 and one rarity 4 pet because the player can only equip two pets.
How exactly would I do that?
Here is my current code:
script.Parent.MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer
for i, v in ipairs(player.PlayerGui.Inventory.Pets.ScrollingFrame:GetDescendants()) do
if v:IsA("IntValue") then
end
end
end)
script.Parent.MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer
local HighestValue
for i, v in ipairs(player.PlayerGui.Inventory.Pets.ScrollingFrame:GetDescendants()) do
if v:IsA("IntValue") then
if not HighestValue then
HighestValue = v
elseif v.Value > HighestValue then
HighestValue = v
end
end
end
end)
Method 2:
script.Parent.MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer
local Values = {}
for i, v in ipairs(player.PlayerGui.Inventory.Pets.ScrollingFrame:GetDescendants()) do
if v:IsA("IntValue") then
table.insert(Values, {v, v.Value})
end
end
table.sort(Values, function(a, b)
return a[2] > b[2]
end)
local HighestPet = Values[1][1]
local HighestValue = Values[1][2]
end)
I went with method 2, and it works. The problem is, this only gives me the highest number, not numbers. I need the two highest numbers, just as I stated above. I would modify your script so that it fits my needs, but I’m not at that level yet.
In method 2, all the values get stores in the Values table, in order of highest to lowest. If you want to get the second highest you can do Values[2][1] and Values[2][2] aswell as my original variables for the first highest.
The script runs through every single pet that has that rarity because it’s a for loop. I’ve been trying to make it work, but I can’t really seem to. How would I stop the for loop after it runs through it once?
script.Parent.MouseButton1Click:Connect(function()
local player = game.Players.LocalPlayer
local Values = {}
for i, v in ipairs(player.PlayerGui.Inventory.Pets.ScrollingFrame:GetDescendants()) do
if v:IsA("IntValue") then
table.insert(Values, {v, v.Value})
end
end
table.sort(Values, function(a, b)
return a[2] > b[2]
end)
local HighestValue = Values[1][2]
local secondHighestValue = Values[2][2]
print(HighestValue)
print(secondHighestValue)
for i, v in ipairs(player.PlayerGui.Inventory.Pets.ScrollingFrame:GetDescendants()) do
if v:IsA("IntValue") and player.Stats.EquippedPets.Value < 2 then
if v.Value == HighestValue then
local petName = v.Parent.Name
local petUUID = v.Parent.UUID.Value
v.Parent.Equipped.Text = "Equipped"
v.Parent.Equipped.TextColor3 = Color3.fromRGB(0, 255, 0)
game.ReplicatedStorage:WaitForChild("PetRE"):WaitForChild("EquipPet"):FireServer(petName,petUUID)
print("1: equipped highest pet")
end
if v.Value == secondHighestValue then
local petName = v.Parent.Name
local petUUID = v.Parent.UUID.Value
v.Parent.Equipped.Text = "Equipped"
v.Parent.Equipped.TextColor3 = Color3.fromRGB(0, 255, 0)
game.ReplicatedStorage:WaitForChild("PetRE"):WaitForChild("EquipPet"):FireServer(petName,petUUID)
print("2: equipped second highest pet")
end
end
end
end)