EquipBest function bugging


The pets that are equipped arent even my best pets^ the yellow mouses are the best pets

script.Parent.EquipBest.Activated:Connect(function()
	local bestDict = {} -- this and the for loop below just makes a table of all pets and how good they are
	
	for i, pet in pairs(scrollFrame:GetChildren()) do
		if pet:IsA("TextButton") then
			if pet.Equipped.Value == true then
				pet.LayoutOrder /= 1000
			end
			pet.Equipped.Value = false
			pet.Checkmark.Visible = false
			bestDict[pet.Name] = -pet.LayoutOrder
		end
	end
	
	ReplicatedStorage.Remotes.UnequipAll:FireServer() -- this unequips all pets
	
	local values = {} -- This and the two things below sort the table
	
	for _, v in pairs(bestDict) do
		table.insert(values,v)
	end
	
	local sortedBestDict = table.sort(values,function(a,b)
		return a > b
	end)
	
	for i = 1, player.OtherStats.MaxPetsEquipped.Value do -- this is what is bugging. This is supposed to equip the first (MaxPetsEquipped) pets in the dictionary
		for key, value in pairs(bestDict) do
			if value == values[i] then
				selectedTemplate = scrollFrame:FindFirstChild(key)
				onEquip()
			end
		end
	end
end)

Well, you go through the bestDict and not even the values which is the sorted table.

script.Parent.EquipBest.Activated:Connect(function()
	local bestDict = {} -- this and the for loop below just makes a table of all pets and how good they are
	
	for i, pet in pairs(scrollFrame:GetChildren()) do
		if pet:IsA("TextButton") then
			if pet.Equipped.Value == true then
				pet.LayoutOrder /= 1000
			end
			pet.Equipped.Value = false
			pet.Checkmark.Visible = false
			bestDict[pet.Name] = -pet.LayoutOrder
		end
	end
	
	ReplicatedStorage.Remotes.UnequipAll:FireServer() -- this unequips all pets
	
	local values = {} -- This and the two things below sort the table
	
	for _, v in pairs(bestDict) do
		table.insert(values,v)
	end
	
	local sortedBestDict = table.sort(values,function(a,b)
		return a > b
	end)
	
	for i = 1, player.OtherStats.MaxPetsEquipped.Value do -- this is what is bugging. This is supposed to equip the first (MaxPetsEquipped) pets in the dictionary
		for key, value in pairs(values) do
			if value == values[i] then
				selectedTemplate = scrollFrame:FindFirstChild(key)
				onEquip()
			end
		end
	end
end)

why would i do for key, value in pairs(values)? values is a list not a dict and is only the values not the names

Ah, my bad. Here is how it would work (changed the way the table values is)

script.Parent.EquipBest.Activated:Connect(function()
	local bestDict = {} -- this and the for loop below just makes a table of all pets and how good they are
	
	for i, pet in pairs(scrollFrame:GetChildren()) do
		if pet:IsA("TextButton") then
			if pet.Equipped.Value == true then
				pet.LayoutOrder /= 1000
			end
			pet.Equipped.Value = false
			pet.Checkmark.Visible = false
			bestDict[pet.Name] = -pet.LayoutOrder
		end
	end
	
	ReplicatedStorage.Remotes.UnequipAll:FireServer() -- this unequips all pets
	
	local values = {} -- This and the two things below sort the table
	
	for i, v in pairs(bestDict) do
		table.insert(values, {key = i, val = v})
	end
	
	local sortedBestDict = table.sort(values,function(a,b)
		return a.val > b.val
	end)
	
	for i = 1, player.OtherStats.MaxPetsEquipped.Value do
		local key = values[i].key
		selectedTemplate = scrollFrame:FindFirstChild(key)
		onEquip()
	end
end)

Also sorry for VERY late response, I had gone to bed at the time and just noticed this topic again