Getting The Biggest Numbers

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)
4 Likes

Two methods of doing this

Method 1:

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)

4 Likes

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)

you can use break at the end of the second if statement to stop the loop

Don’t mean to be disrespectful but don’t you think that I’ve already tried that? That’s the reason why I haven’t replied for an hour.

What’s preventing you from just using this

Nothing. But the result would be the same. The problem isn’t with the table.sort. The problem is I can’t stop the for loop.

Since for some reason break doesn’t work and that’s the end of the function, couldn’t you just use return?

I figured it out. I just had to put the second highest value in another for loop. And use break on both.

1 Like

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